Skip to content

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:

This was referenced Oct 5, 2021
Linda423 added a commit to Linda423/React that referenced this pull request Jul 31, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
genie4viz pushed a commit to genie4viz/react that referenced this pull request Sep 2, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
Dosant added a commit to elastic/kibana that referenced this pull request Sep 9, 2024
## Summary

Part of #138222

in @types/react@18 types [have become more
strict](DefinitelyTyped/DefinitelyTyped#56210).
This PR addresses a bunch of easy fixes. The most common are:

### 1 Removal of implicit children

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

```diff
 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>
```

or 

```diff
- const SomeFunctionComponent: React.FunctionComponent<Props> = props => <div>{props.children}</div>
+ const SomeFunctionComponent: React.FunctionComponent<React.PropsWithChildren<Props>> = props => <div>{props.children}</div>
```


*Note 1:*
The most common change occurs in unit tests where `renderHook` and
`wrapper` are used. I had to re-type the props so that `children` were
there

```diff
const { result } = renderHook(
         () => {
           return useLicense();
         },
         {
-           wrapper: ({ children }) => (
+           wrapper: ({ children }: React.PropsWithChildren<{}>) => (
             <TestProviders license={license}>{children}</TestProviders>
           ),
         }
       );
```

```diff
- const { result } = renderHook<GetCasesColumn, UseCasesColumnsReturnValue>(
+ const { result } = renderHook<React.PropsWithChildren<GetCasesColumn>, UseCasesColumnsReturnValue>(
       () => useCasesColumns(defaultColumnArgs),
       {
         wrapper: ({ children }) => <TestProviders>{children}</TestProviders>,
       }
     );
```



*Note 2:*
With @types/react@17 the components that don't have any props apart from
`children` I had to use `React.PropsWithChildren<{}>`, whereas in
@types/react@18 the argument becomes optional, so it can be omitted, and
type simpler with `React.PropsWithChildren` without an argument



### 2 `this.context` becomes `unknown` (was `any`)

In a couple of places where `this.context` is used, I had to type it

### 3 `ComponentType` from enzyme is no longer compatible with
`ComponentType` from react

This one is a bummer, but where `wrappingComponent` with enzyme is used
I had to cast it to type from `enzyme`

```diff
- import { mount } from 'enzyme'
+ import { mount, ComponentType } from 'enzyme'

 wrapper = mount(<ClosureOptions {...props} />, {
-       wrappingComponent: TestProviders,
+       wrappingComponent: TestProviders as ComponentType<{}>,
});
```
Dosant added a commit to elastic/kibana that referenced this pull request Sep 12, 2024
## Summary

Part of #138222

in @types/react@18 types
DefinitelyTyped/DefinitelyTyped#56210. This PR
addresses a bunch of remaining fixes **(hopefully the last mass ping PR
like this)** The most common are:


### 1 Objects are no longer considered a valid ReactNode

In types@17 the ReactNode typing was too soft, it allowed objects and
functions being passed as ReactNode, e.g.

```
let obj: React.ReactNode = {};
let func: React.ReactNode = () => {};
```

This was by mistake, and this PR mutes most of such cases by simply
casting to a `string` or `ReactNode`.
In some cases, it is worth to follow up and address the raised issues in
a better way (see in comments)


```diff

function MyComponent() {

const error: string | Error = 'Error'

return (
  <div>
-   {error}
+   {error as string}
  </div>
)

}

```


Most common problems are related to rendering errors, where it could be
`string | Error` object rendered directly as a ReactNode. Most often it
is related to alerting framework:

```
export interface RuleFormParamsErrors {
  [key: string]: string | string[] | RuleFormParamsErrors;
}
```

Not sure if there is a better fix then casting, surely not short-term. 

### 2 More `useCallback` implicit any fixes

Follow up to #191659

### 3 `EuiSelect` doesn't have a placeholder prop 

In a couple of places, the `placeholder` prop was removed. This is
because react types were updated and `placeholder` was removed from the
base HTML element, so it highlighted places where `placeholder` prop was
redundant
ericlamb added a commit to ericlamb/redux-oidc that referenced this pull request Sep 20, 2024
In React 18 implicit children were removed from components. Components that utilize children need to declare their types directly.

See: DefinitelyTyped/DefinitelyTyped#56210
ericlamb added a commit to ericlamb/redux-oidc that referenced this pull request Sep 20, 2024
In React 18 implicit children were removed from components. Components that utilize children need to declare their types directly.

See: DefinitelyTyped/DefinitelyTyped#56210

#79
eagle2722 added a commit to eagle2722/Recoil that referenced this pull request Sep 21, 2024
Summary:
Fixes facebookexperimental/Recoil#1717

Since types/react v18, Implicit `children` was removed from `React.FC`. This change works for types/react v18 and earlier.
DefinitelyTyped/DefinitelyTyped#56210

Pull Request resolved: facebookexperimental/Recoil#1718

Reviewed By: drarmstr

Differential Revision: D35501855

Pulled By: mondaychen

fbshipit-source-id: cf469b96a5220966718270e43e3f0503e144acf3
dreamsoft07 added a commit to dreamsoft07/React-TypeScript that referenced this pull request Oct 15, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

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

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
super-dev03 pushed a commit to super-dev03/react-test that referenced this pull request Dec 25, 2024
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
Mani2044 added a commit to Mani2044/react that referenced this pull request Feb 6, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
sunstrike-spec added a commit to sunstrike-spec/react that referenced this pull request Feb 23, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
darkhorse00512 added a commit to darkhorse00512/React that referenced this pull request Feb 28, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
Misha0421 added a commit to Misha0421/React-Project that referenced this pull request Mar 4, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
buddy0323 added a commit to buddy0323/react that referenced this pull request Mar 11, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
reo0603 added a commit to reo0603/TypeScript-Cheatsheet that referenced this pull request Apr 10, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
X-CoCreater pushed a commit to X-CoCreater/SWR that referenced this pull request May 17, 2025
fast-codi-expert added a commit to fast-codi-expert/Creating-Sheet that referenced this pull request May 18, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
tanaka0722-dev added a commit to tanaka0722-dev/react that referenced this pull request Oct 14, 2025
* feat: add information about change in React typing

See DefinitelyTyped/DefinitelyTyped#56210.

* chore: change wording
software-dev-web added a commit to software-dev-web/react-experience that referenced this pull request Dec 11, 2025
* 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