Emotion: TypeScript example is broken in documentation

Created on 31 May 2018  路  2Comments  路  Source: emotion-js/emotion

When trying to run this typescript example from documentation, I get many typescript errors
https://emotion.sh/docs/typescript#passing-props-when-styling-a-react-component

  • emotion version: 9.1.3
  • react version: 16.4.0
  • typescript version: 2.8.3

What you did:
Tried to run this example

What happened:
Got many typescript errors

screen shot 2018-05-31 at 16 55 27
screen shot 2018-05-31 at 16 56 34
screen shot 2018-05-31 at 16 57 19
screen shot 2018-05-31 at 16 58 18

TypeScript

Most helpful comment

Ok, figured out the correct way

import * as React from 'react'
import styled from 'react-emotion'

type ComponentProps = {
  className?: string,
  label: string
}

type StyledComponentProps = {
  bgColor: string
}

const Component: React.SFC<ComponentProps> = ({ label, className }) => (
  <div className={className}>{label}</div>
)

const StyledComponent = styled<ComponentProps, StyledComponentProps>(Component)`
  color: red;
  background: ${props => props.bgColor};
`

const App = () => (
  <StyledComponent bgColor="red" label="Oh, needs to re-type label prop =(" />
)

All 2 comments

Ok, figured out the correct way

import * as React from 'react'
import styled from 'react-emotion'

type ComponentProps = {
  className?: string,
  label: string
}

type StyledComponentProps = {
  bgColor: string
}

const Component: React.SFC<ComponentProps> = ({ label, className }) => (
  <div className={className}>{label}</div>
)

const StyledComponent = styled<ComponentProps, StyledComponentProps>(Component)`
  color: red;
  background: ${props => props.bgColor};
`

const App = () => (
  <StyledComponent bgColor="red" label="Oh, needs to re-type label prop =(" />
)

Close this issue in favor of #679.

Was this page helpful?
0 / 5 - 0 ratings