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

[react] React 18 types #56210

Merged
merged 29 commits into from Apr 7, 2022

Conversation

eps1lon
Copy link
Collaborator

@eps1lon eps1lon commented Oct 5, 2021

Overwhelmed by compile errors after upgrading to @types/react@^18.0.0? Check out https://github.com/eps1lon/types-react-codemod.

Upgrading to React 18 with TypeScript

Breaking Changes

Removal of implicit children

For components that do implement children but relied on their implicit declaration from React.FunctionComponent or React.Component:

 interface Props {
+  children?: React.ReactNode;
 }

 class SomeClassComponents React.Component<Props> {
   render() {
     return  <div>{this.props.children}</div>
   }
 }
 const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>

Please check Removal Of Implicit Children for the rationale.
I may have a solution to bring these back on-demand without patching the published types (watch #59751)

Remove {} from ReactFragment

This was never correct and mostly required to interop with implicit children.

this.context becomes unknown

Was any before. The community mostly prefers unknown by default (any would silently be unsound).

Restore old behavior:

 class Component extends React.Component {
+ context: any;
   render() {
     return this.context.value;
   }
 }

### `noImplicitAny` and `useCallback`

Enabling `noImplicitAny` in `tsconfig` (on by default with `strict: true`) will not type-check if an explicit type is omitted in `useCallback`.

```diff
+ // @ts-expect-error -- `event` as implicitly type 'any'
 useCallback((event) => {})

Remove deprecated types

Bringing the terminology more in line with how they're called in the React docs and repository.

-StatelessComponent
+FunctionComponent

-SFC
+FC

-React.ReactType
+React.ElementType

-SFCElement
+FunctionComponentElement

// no replacement
-Props
-RefForwardingComponent
-SFCFactory

Issues

Closes #46691
Closes #34237
Closes #56026
Closes #52873

Follow-up:

@typescript-bot typescript-bot added this to Needs Author Action in New Pull Request Status Board Oct 5, 2021
This was referenced Oct 5, 2021
@raegen
Copy link

raegen commented Jul 3, 2023

Considering the pivotal argument for the implicit children change is that all the other excess props are rejected so why would we treat children any differently.
How about the fact that no other prop, excess or not, is accessed, processed, handled in any particular way by react, yet children is?
How about the fact that furthermore, children prop has some very specific rules it must abide, depending on its type - the scope of key prop are ultimately siblings, yet it is enforced through children?
How about the fact that no other prop is explicitly frozen (Object.freeze) yet children is?
How about the fact that no other prop facilitates construction of the DOM tree nor attachment to it?

Screenshot 2023-07-03 at 22 14 13

I get it. You like it simple, generic, elegant. Exceptions hurt, are ugly and nobody likes them. But the thing is, whether we like them or not doesn't change the actual engine this is supposed to annotate and it is this exact change that makes children implicit. It blatantly ignores and obfuscates behaviour that's an integral part of the API. Apart from that, the only purpose this served was that of an impediment to updating react to 18.

This whole endeavour is quite simply flabberghasting.

@Hypnosphi
Copy link

Hypnosphi commented Jul 4, 2023

Still, if your component doesn't use children prop in any way, passing it should be an error

whiteghostDev added a commit to whiteghostDev/Awesome-React that referenced this pull request Aug 6, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
codechieflee added a commit to codechieflee/react-hook-swr that referenced this pull request Aug 19, 2023
cedev935 pushed a commit to cedev935/React-TypeScript that referenced this pull request Sep 11, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
muscliary pushed a commit to muscliary/swr that referenced this pull request Sep 12, 2023
aleksandaralek added a commit to aleksandaralek/typescript-react-cheatsheet that referenced this pull request Oct 24, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
xbucks pushed a commit to xbucks/react-cheatsheets that referenced this pull request Oct 24, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
le-pus pushed a commit to le-pus/React-Typescript that referenced this pull request Oct 26, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
atsumi000105 added a commit to atsumi000105/typescript-cheatsheets-react that referenced this pull request Dec 8, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
secretsuperstar1109 added a commit to secretsuperstar1109/react-typescript-cheatsheets that referenced this pull request Dec 9, 2023
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
champion119 added a commit to champion119/react that referenced this pull request Jan 5, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
forbesus added a commit to forbesus/OpenEmojiBattler that referenced this pull request Feb 18, 2024
SManOlaf38 pushed a commit to SManOlaf38/react_typescript that referenced this pull request Feb 20, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
SManOlaf38 added a commit to SManOlaf38/react_typescript that referenced this pull request Feb 20, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
rising-dragon360 added a commit to rising-dragon360/react that referenced this pull request Mar 13, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
EugeneYoona added a commit to EugeneYoona/React_full_src that referenced this pull request Apr 10, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
snowMan128 added a commit to snowMan128/TypeReact that referenced this pull request Apr 15, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
fairskyDev0201 added a commit to fairskyDev0201/typescript-cheatsheet that referenced this pull request Apr 17, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
bluesky4293 added a commit to bluesky4293/React that referenced this pull request Apr 27, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Check Config Changes a module config files Critical package Edits multiple packages Huge Change Maintainer Approved Self Merge This PR can now be self-merged by the PR author or an owner Too Many Owners
Projects
None yet