Skip to content

Commit

Permalink
fix(react): fixes for --exactOptionalPropertyTypes TS flag (#827)
Browse files Browse the repository at this point in the history
TypeScript 4.4 introduced new `--exactOptionalPropertyTypes` flag. With this flag enabled typescript compiler strictly distinguish between `?` and `undefined` type. Currently `@types/react:17.0` package has `className?: string | undefined` and `style?: CSSProperties | undefined` specified for html attributes, but linaria types are only marked as `?` but `undefined`. This patch fixes this incompatibility.
  • Loading branch information
geakstr committed Aug 31, 2021
1 parent 6a062e1 commit eed92b1
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/react/src/styled.ts
Expand Up @@ -63,13 +63,15 @@ interface IProps {
// If styled wraps custom component, that component should have className property
function styled<TConstructor extends React.FunctionComponent<any>>(
tag: TConstructor extends React.FunctionComponent<infer T>
? T extends { className?: string }
? T extends { className?: string | undefined }
? TConstructor
: never
: never
): ComponentStyledTag<TConstructor>;
function styled<T>(
tag: T extends { className?: string } ? React.ComponentType<T> : never
tag: T extends { className?: string | undefined }
? React.ComponentType<T>
: never
): ComponentStyledTag<T>;
function styled<TName extends keyof JSX.IntrinsicElements>(
tag: TName
Expand Down Expand Up @@ -197,7 +199,7 @@ type ComponentStyledTag<T> = <
>(
strings: TemplateStringsArray,
// Expressions can contain functions only if wrapped component has style property
...exprs: TrgProps extends { style?: React.CSSProperties }
...exprs: TrgProps extends { style?: React.CSSProperties | undefined }
? Array<
| StaticPlaceholder
| ((props: NoInfer<OwnProps & TrgProps>) => string | number)
Expand Down

0 comments on commit eed92b1

Please sign in to comment.