You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* fixes usage of deprecated JSX.Element to React.JSX.Element typescript-cheatsheets#643
* gen-readme
* Migrate away from all usage of deprecated global JSX
* one more
---------
Co-authored-by: eps1lon <[email protected]>
<summary><b><code>JSX.LibraryManagedAttributes</code> nuance for library authors</b></summary>
924
+
<summary><b><code>React.JSX.LibraryManagedAttributes</code> nuance for library authors</b></summary>
925
925
926
926
The above implementations work fine for App creators, but sometimes you want to be able to export `GreetProps` so that others can consume it. The problem here is that the way `GreetProps` is defined, `age` is a required prop when it isn't because of `defaultProps`.
927
927
928
-
The insight to have here is that [`GreetProps` is the _internal_ contract for your component, not the _external_, consumer facing contract](https://github.com/typescript-cheatsheets/react/issues/66#issuecomment-453878710). You could create a separate type specifically for export, or you could make use of the `JSX.LibraryManagedAttributes` utility:
928
+
The insight to have here is that [`GreetProps` is the _internal_ contract for your component, not the _external_, consumer facing contract](https://github.com/typescript-cheatsheets/react/issues/66#issuecomment-453878710). You could create a separate type specifically for export, or you could make use of the `React.JSX.LibraryManagedAttributes` utility:
929
929
930
930
```tsx
931
931
// internal contract, should not be exported out
@@ -938,7 +938,7 @@ class Greet extends Component<GreetProps> {
@@ -1044,7 +1044,7 @@ export class MyComponent extends React.Component<IMyComponentProps> {
1044
1044
}
1045
1045
```
1046
1046
1047
-
The problem with this approach is it causes complex issues with the type inference working with `JSX.LibraryManagedAttributes`. Basically it causes the compiler to think that when creating a JSX expression with that component, that all of its props are optional.
1047
+
The problem with this approach is it causes complex issues with the type inference working with `React.JSX.LibraryManagedAttributes`. Basically it causes the compiler to think that when creating a JSX expression with that component, that all of its props are optional.
1048
1048
1049
1049
[See commentary by @ferdaber here](https://github.com/typescript-cheatsheets/react/issues/57) and [here](https://github.com/typescript-cheatsheets/react/issues/61).
1050
1050
@@ -1143,7 +1143,7 @@ Relevant for components that accept other React components as props.
1143
1143
```tsx
1144
1144
exportdeclareinterfaceAppProps {
1145
1145
children?:React.ReactNode; // best, accepts everything React can render
1146
-
childrenElement:JSX.Element; // A single React element
1146
+
childrenElement:React.JSX.Element; // A single React element
1147
1147
style?:React.CSSProperties; // to pass through style props
1148
1148
onChange?:React.FormEventHandler<HTMLInputElement>; // form events! the generic parameter is the type of event.target
1149
1149
// more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
@@ -1179,16 +1179,16 @@ This is because `ReactNode` includes `ReactFragment` which allowed type `{}` bef
1179
1179
</details>
1180
1180
1181
1181
<details>
1182
-
<summary><b>JSX.Element vs React.ReactNode?</b></summary>
1182
+
<summary><b>React.JSX.Element vs React.ReactNode?</b></summary>
1183
1183
1184
-
Quote [@ferdaber](https://github.com/typescript-cheatsheets/react/issues/57): A more technical explanation is that a valid React node is not the same thing as what is returned by `React.createElement`. Regardless of what a component ends up rendering, `React.createElement` always returns an object, which is the `JSX.Element` interface, but `React.ReactNode` is the set of all possible return values of a component.
1184
+
Quote [@ferdaber](https://github.com/typescript-cheatsheets/react/issues/57): A more technical explanation is that a valid React node is not the same thing as what is returned by `React.createElement`. Regardless of what a component ends up rendering, `React.createElement` always returns an object, which is the `React.JSX.Element` interface, but `React.ReactNode` is the set of all possible return values of a component.
1185
1185
1186
-
-`JSX.Element` -> Return value of `React.createElement`
1186
+
-`React.JSX.Element` -> Return value of `React.createElement`
1187
1187
-`React.ReactNode` -> Return value of a component
1188
1188
1189
1189
</details>
1190
1190
1191
-
[More discussion: Where ReactNode does not overlap with JSX.Element](https://github.com/typescript-cheatsheets/react/issues/129)
1191
+
[More discussion: Where ReactNode does not overlap with React.JSX.Element](https://github.com/typescript-cheatsheets/react/issues/129)
1192
1192
1193
1193
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react/issues/new).
Looking at [the source for `ComponentProps`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f3134f4897c8473f590cbcdd5788da8d59796f45/types/react/index.d.ts#L821) shows that this is a clever wrapper for `JSX.IntrinsicElements`, whereas the second method relies on specialized interfaces with unfamiliar naming/capitalization.
66
+
Looking at [the source for `ComponentProps`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/f3134f4897c8473f590cbcdd5788da8d59796f45/types/react/index.d.ts#L821) shows that this is a clever wrapper for `React.JSX.IntrinsicElements`, whereas the second method relies on specialized interfaces with unfamiliar naming/capitalization.
67
67
68
68
> Note: There are over 50 of these specialized interfaces available - look for `HTMLAttributes` in our [`@types/react` commentary](https://react-typescript-cheatsheet.netlify.app/docs/advanced/types_react_api#typesreact).
69
69
@@ -375,7 +375,7 @@ Parent.propTypes = {
375
375
376
376
The thing you cannot do is **specify which components** the children are, e.g. If you want to express the fact that "React Router `<Routes>` can only have `<Route>` as children, nothing else is allowed" in TypeScript.
377
377
378
-
This is because when you write a JSX expression (`const foo = <MyComponent foo='foo' />`), the resultant type is blackboxed into a generic JSX.Element type. (_[thanks @ferdaber](https://github.com/typescript-cheatsheets/react/issues/271)_)
378
+
This is because when you write a JSX expression (`const foo = <MyComponent foo='foo' />`), the resultant type is blackboxed into a generic React.JSX.Element type. (_[thanks @ferdaber](https://github.com/typescript-cheatsheets/react/issues/271)_)
379
379
380
380
## Type Narrowing based on Props
381
381
@@ -414,8 +414,8 @@ type AnchorProps = React.AnchorHTMLAttributes<HTMLAnchorElement> & {
414
414
415
415
// Input/output options
416
416
typeOverload= {
417
-
(props:ButtonProps):JSX.Element;
418
-
(props:AnchorProps):JSX.Element;
417
+
(props:ButtonProps):React.JSX.Element;
418
+
(props:AnchorProps):React.JSX.Element;
419
419
};
420
420
421
421
// Guard to check if href exists in props
@@ -438,8 +438,8 @@ Components, and JSX in general, are analogous to functions. When a component can
438
438
A very common use case for this is to render something as either a button or an anchor, based on if it receives a `href` attribute.
Copy file name to clipboardExpand all lines: docs/advanced/types-react-ap.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@ Not Commonly Used but Good to know
37
37
-`ComponentProps` - props of a component - most useful for [Wrapping/Mirroring a HTML Element](https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase#wrappingmirroring-a-html-element)
38
38
-`ComponentPropsWithRef` - props of a component where if it is a class-based component it will replace the `ref` prop with its own instance type
39
39
-`ComponentPropsWithoutRef` - props of a component without its `ref` prop
40
-
-`HTMLProps` and `HTMLAttributes` - these are the most generic versions, for global attributes (see a list of [attributes marked as "global attribute" on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes)). In general, prefer `React.ComponentProps`, `JSX.IntrinsicElements`, or [specialized HTMLAttributes interfaces](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a2aa0406e7bf269eef01292fcb2b24dee89a7d2b/types/react/index.d.ts#L1914-L2625):
40
+
-`HTMLProps` and `HTMLAttributes` - these are the most generic versions, for global attributes (see a list of [attributes marked as "global attribute" on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes)). In general, prefer `React.ComponentProps`, `React.JSX.IntrinsicElements`, or [specialized HTMLAttributes interfaces](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a2aa0406e7bf269eef01292fcb2b24dee89a7d2b/types/react/index.d.ts#L1914-L2625):
41
41
42
42
<details>
43
43
<summary>
@@ -102,7 +102,7 @@ Note that there are about 50 of these, which means there are some HTML elements
102
102
103
103
- all methods: `createElement`, `cloneElement`, ... are all public and reflect the React runtime API
104
104
105
-
[@Ferdaber's note](https://github.com/typescript-cheatsheets/react/pull/69): I discourage the use of most `...Element` types because of how black-boxy `JSX.Element` is. You should almost always assume that anything produced by `React.createElement` is the base type `React.ReactElement`.
105
+
[@Ferdaber's note](https://github.com/typescript-cheatsheets/react/pull/69): I discourage the use of most `...Element` types because of how black-boxy `React.JSX.Element` is. You should almost always assume that anything produced by `React.createElement` is the base type `React.ReactElement`.
Copy file name to clipboardExpand all lines: docs/basic/getting-started/basic-type-examples.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ Relevant for components that accept other React components as props.
88
88
```tsx
89
89
exportdeclareinterfaceAppProps {
90
90
children?:React.ReactNode; // best, accepts everything React can render
91
-
childrenElement:JSX.Element; // A single React element
91
+
childrenElement:React.JSX.Element; // A single React element
92
92
style?:React.CSSProperties; // to pass through style props
93
93
onChange?:React.FormEventHandler<HTMLInputElement>; // form events! the generic parameter is the type of event.target
94
94
// more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
@@ -124,16 +124,16 @@ This is because `ReactNode` includes `ReactFragment` which allowed type `{}` bef
124
124
</details>
125
125
126
126
<details>
127
-
<summary><b>JSX.Element vs React.ReactNode?</b></summary>
127
+
<summary><b>React.JSX.Element vs React.ReactNode?</b></summary>
128
128
129
-
Quote [@ferdaber](https://github.com/typescript-cheatsheets/react/issues/57): A more technical explanation is that a valid React node is not the same thing as what is returned by `React.createElement`. Regardless of what a component ends up rendering, `React.createElement` always returns an object, which is the `JSX.Element` interface, but `React.ReactNode` is the set of all possible return values of a component.
129
+
Quote [@ferdaber](https://github.com/typescript-cheatsheets/react/issues/57): A more technical explanation is that a valid React node is not the same thing as what is returned by `React.createElement`. Regardless of what a component ends up rendering, `React.createElement` always returns an object, which is the `React.JSX.Element` interface, but `React.ReactNode` is the set of all possible return values of a component.
130
130
131
-
-`JSX.Element` -> Return value of `React.createElement`
131
+
-`React.JSX.Element` -> Return value of `React.createElement`
132
132
-`React.ReactNode` -> Return value of a component
133
133
134
134
</details>
135
135
136
-
[More discussion: Where ReactNode does not overlap with JSX.Element](https://github.com/typescript-cheatsheets/react/issues/129)
136
+
[More discussion: Where ReactNode does not overlap with React.JSX.Element](https://github.com/typescript-cheatsheets/react/issues/129)
137
137
138
138
[Something to add? File an issue](https://github.com/typescript-cheatsheets/react/issues/new).
Copy file name to clipboardExpand all lines: docs/basic/getting-started/default-props.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -80,11 +80,11 @@ let el = <Greet age={3} />;
80
80
```
81
81
82
82
<details>
83
-
<summary><b><code>JSX.LibraryManagedAttributes</code> nuance for library authors</b></summary>
83
+
<summary><b><code>React.JSX.LibraryManagedAttributes</code> nuance for library authors</b></summary>
84
84
85
85
The above implementations work fine for App creators, but sometimes you want to be able to export `GreetProps` so that others can consume it. The problem here is that the way `GreetProps` is defined, `age` is a required prop when it isn't because of `defaultProps`.
86
86
87
-
The insight to have here is that [`GreetProps` is the _internal_ contract for your component, not the _external_, consumer facing contract](https://github.com/typescript-cheatsheets/react/issues/66#issuecomment-453878710). You could create a separate type specifically for export, or you could make use of the `JSX.LibraryManagedAttributes` utility:
87
+
The insight to have here is that [`GreetProps` is the _internal_ contract for your component, not the _external_, consumer facing contract](https://github.com/typescript-cheatsheets/react/issues/66#issuecomment-453878710). You could create a separate type specifically for export, or you could make use of the `React.JSX.LibraryManagedAttributes` utility:
88
88
89
89
```tsx
90
90
// internal contract, should not be exported out
@@ -97,7 +97,7 @@ class Greet extends Component<GreetProps> {
@@ -203,7 +203,7 @@ export class MyComponent extends React.Component<IMyComponentProps> {
203
203
}
204
204
```
205
205
206
-
The problem with this approach is it causes complex issues with the type inference working with `JSX.LibraryManagedAttributes`. Basically it causes the compiler to think that when creating a JSX expression with that component, that all of its props are optional.
206
+
The problem with this approach is it causes complex issues with the type inference working with `React.JSX.LibraryManagedAttributes`. Basically it causes the compiler to think that when creating a JSX expression with that component, that all of its props are optional.
207
207
208
208
[See commentary by @ferdaber here](https://github.com/typescript-cheatsheets/react/issues/57) and [here](https://github.com/typescript-cheatsheets/react/issues/61).
0 commit comments