Skip to content

Commit

Permalink
Improved Flow type inference of props for inline functions passed to …
Browse files Browse the repository at this point in the history
…the `styled` factory (#2199)

* fix: make Flow infer prop types for inline styled functions

* fix: update flow ignore comment

* Update .changeset/rare-beans-think.md

Co-authored-by: Mitchell Hamilton <mitchell@hamil.town>
Co-authored-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
  • Loading branch information
3 people committed Mar 12, 2021
1 parent 5bf3522 commit 734b36b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-beans-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@emotion/styled': patch
---

Improved Flow type inference of props for inline functions passed to the `styled` factory.
12 changes: 12 additions & 0 deletions packages/styled/flow-tests/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ const validProp = <Div color="red" />

// $FlowFixMe: expect error - color property should be a string
const invalidProp = <Div color={2} />

styled<Props>(props => <div className={props.className}>{props.color}</div>)`
color: ${props => props.color};
`

styled<Props>(props => {
const color: string = props.color
const className: string = props.className
// $FlowFixMe: expect error - color should be a string
const colorTest: number = props.color
return <div className={props.className}>{props.color}</div>
})``
6 changes: 3 additions & 3 deletions packages/styled/src/base.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// @flow
import * as React from 'react'
import type { ElementType } from 'react'
import {
getDefaultShouldForwardProp,
composeShouldForwardProps,
type StyledOptions,
type CreateStyled,
type PrivateStyledComponent
type PrivateStyledComponent,
type StyledElementType
} from './utils'
import { withEmotionCache, ThemeContext } from '@emotion/react'
import { getRegisteredStyles, insertStyles } from '@emotion/utils'
Expand Down Expand Up @@ -185,7 +185,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
})

Styled.withComponent = (
nextTag: ElementType,
nextTag: StyledElementType<Props>,
nextOptions?: StyledOptions
) => {
return createStyled(nextTag, {
Expand Down
14 changes: 11 additions & 3 deletions packages/styled/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
// @flow
import type { ElementType, StatelessFunctionalComponent } from 'react'
import type {
ElementType,
StatelessFunctionalComponent,
AbstractComponent
} from 'react'
import isPropValid from '@emotion/is-prop-valid'

export type Interpolations = Array<any>

export type StyledElementType<Props> =
| string
| AbstractComponent<{ ...Props, className: string }, mixed>

export type StyledOptions = {
label?: string,
shouldForwardProp?: string => boolean,
Expand All @@ -14,7 +22,7 @@ export type StyledComponent<Props> = StatelessFunctionalComponent<Props> & {
defaultProps: any,
toString: () => string,
withComponent: (
nextTag: ElementType,
nextTag: StyledElementType<Props>,
nextOptions?: StyledOptions
) => StyledComponent<Props>
}
Expand Down Expand Up @@ -67,7 +75,7 @@ export type CreateStyledComponent = <Props>(

export type CreateStyled = {
<Props>(
tag: ElementType,
tag: StyledElementType<Props>,
options?: StyledOptions
): (...args: Interpolations) => StyledComponent<Props>,
[key: string]: CreateStyledComponent,
Expand Down

0 comments on commit 734b36b

Please sign in to comment.