Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JSX.ElementType #3048

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/wicked-boxes-lick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@emotion/react": patch
---

Added `ElementType` to the Emotion's `JSX` namespace. It's defined in the same way as the one in `@types/react` and should make it possible to use components that return `string`s, `Promise`s and other types that are valid in React.
1 change: 1 addition & 0 deletions packages/react/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export function ClassNames(props: ClassNamesProps): ReactElement
export const jsx: typeof createElement
export namespace jsx {
namespace JSX {
type ElementType = EmotionJSX.ElementType
interface Element extends EmotionJSX.Element {}
interface ElementClass extends EmotionJSX.ElementClass {}
interface ElementAttributesProperty
Expand Down
5 changes: 5 additions & 0 deletions packages/react/types/jsx-namespace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ type ReactJSXIntrinsicAttributes = JSX.IntrinsicAttributes
type ReactJSXIntrinsicClassAttributes<T> = JSX.IntrinsicClassAttributes<T>
type ReactJSXIntrinsicElements = JSX.IntrinsicElements

// based on the code from @types/react@18.2.8
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/3197efc097d522c4bf02b94e1a0766d007d6cdeb/types/react/index.d.ts#LL3204C13-L3204C13
type ReactJSXElementType = string | React.JSXElementConstructor<any>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, likely we could do some hacks with @ts-ignore to "detect" if the type is there in React's JSX but perhaps it's not worth it and copy-paste is fine. I'll give this a thought.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I feel like copying it is probably the right approach tbh


export namespace EmotionJSX {
type ElementType = ReactJSXElementType
naari3 marked this conversation as resolved.
Show resolved Hide resolved
interface Element extends ReactJSXElement {}
interface ElementClass extends ReactJSXElementClass {}
interface ElementAttributesProperty
Expand Down
16 changes: 0 additions & 16 deletions packages/react/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,19 +245,3 @@ const anim1 = keyframes`
? true
: false
}

// RMWC-like component test
declare const OtherComponent: {
<Tag extends React.ElementType>(
props:
| React.AllHTMLAttributes<HTMLInputElement>
| React.ComponentPropsWithRef<Tag>,
ref: any
): JSX.Element
displayName?: string
}
;<OtherComponent
onChange={ev => {
console.log(ev.currentTarget.value)
}}
/>