From 8aacec330126747f9ab789c2c9e2b342a8259941 Mon Sep 17 00:00:00 2001 From: Emma Hamilton Date: Thu, 12 Dec 2024 11:00:55 +1000 Subject: [PATCH] Fix TypeScript error when writing higher order components while using Emotion's JSX namespace --- .changeset/nervous-pigs-reflect.md | 5 +++ packages/react/src/jsx-namespace.ts | 19 +++++---- packages/react/types/tests.tsx | 60 +++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 8 deletions(-) create mode 100644 .changeset/nervous-pigs-reflect.md diff --git a/.changeset/nervous-pigs-reflect.md b/.changeset/nervous-pigs-reflect.md new file mode 100644 index 0000000000..fe2952dcb4 --- /dev/null +++ b/.changeset/nervous-pigs-reflect.md @@ -0,0 +1,5 @@ +--- +'@emotion/react': patch +--- + +Fix TypeScript error when writing higher order components while using `@emotion/react` `jsx` pragma/`jsxImportSource` diff --git a/packages/react/src/jsx-namespace.ts b/packages/react/src/jsx-namespace.ts index 5bfd22c1e4..a6c6820493 100644 --- a/packages/react/src/jsx-namespace.ts +++ b/packages/react/src/jsx-namespace.ts @@ -6,11 +6,15 @@ type IsPreReact19 = 2 extends Parameters>['length'] ? true : false -type WithConditionalCSSProp

= 'className' extends keyof P - ? string extends P['className' & keyof P] - ? { css?: Interpolation } - : {} - : {} +type WithConditionalCSSProp

= + | (P extends unknown + ? 'className' extends keyof P + ? string extends P['className' & keyof P] + ? { css?: Interpolation } + : {} + : {} + : {}) + | {} // unpack all here to avoid infinite self-referencing when defining our own JSX namespace for the pre-React 19 case @@ -91,9 +95,8 @@ export namespace EmotionJSX { export interface ElementChildrenAttribute extends ReactJSXElementChildrenAttribute {} - export type LibraryManagedAttributes = P extends unknown - ? WithConditionalCSSProp

& ReactJSXLibraryManagedAttributes - : never + export type LibraryManagedAttributes = WithConditionalCSSProp

& + ReactJSXLibraryManagedAttributes export interface IntrinsicAttributes extends ReactJSXIntrinsicAttributes {} export interface IntrinsicClassAttributes diff --git a/packages/react/types/tests.tsx b/packages/react/types/tests.tsx index fda9729ea1..f86019f1a6 100644 --- a/packages/react/types/tests.tsx +++ b/packages/react/types/tests.tsx @@ -255,3 +255,63 @@ const anim1 = keyframes` // $ExpectError ; } + +{ + const withSomething = ( + SomeComponent: (props: Props & { something: string }) => React.ReactNode + ) => { + return (props: Props) => { + return + } + } + const RendersSomething = withSomething(props =>

{props.something}
) + ; + + const WithSomeStyle = ( + SomeComponent: (props: Props) => React.ReactNode + ) => { + return (props: Props) => { + return ( + + css={{ direction: 'rtl' }} + /> + ) + } + } + const Something = WithSomeStyle((props: { className?: string }) => null) + ; + ;( + SomeComponent: (props: Props) => React.ReactNode + ) => { + return (props: Props) => { + return ( + + ) + } + } + ;(SomeComponent: (props: Props) => React.ReactNode) => { + return (props: Props) => { + return ( + + ) + } + } +}