We are trying to migrate to react 18.
The children prop was removed from React.FunctionComponent (React.FC) so you have to declare it explicitly in your component properties.
But we want to keep the old React.FunctionComponent with children behavior. So, we are tryibg to override react types by creating a custom type definition file index.d.ts. This way we wont have to change 100s of components manually.
import * as React from '@types/react';
declare module 'react' {
//type PropsWithChildren<P> = P & { children?: ReactNode | undefined };
interface FunctionComponent<P = {}> {
(props: PropsWithChildren<P>, context?: any): ReactElement<any, any> | null;
}
}
We have components like below.
interface IProps {
age: number
}
const Demo: React.FunctionComponent<IProps> = ({
children,
age
}) => (
<>
{children}
{age}
</>
);
<Demo age={10}>Hi</Demo>
After overriding react types we are now getting following error in above Demo component.
Property 'children' does not exist on type 'IProps | PropsWithChildren<IProps>'
PropsWithChildren is defined in React 18 as below.
type PropsWithChildren<P> = P & { children?: ReactNode | undefined };
Below is the codesandbox link
https://codesandbox.io/s/quizzical-goodall-9d9s3j?file=/src/Demo.tsx
Ref :- React 18 TypeScript children FC
Any help?