跳到主内容

Property 'children' does not exist on type 'IPageProps'.

· 1分钟阅读

问题

今天心血来潮,把项目升级到 react18 版本,今天运行发现报错了,Property 'children' does not exist on type 'IPageProps'. 旧的版本,children 属性会在 FC 接口声明,自己添加 chilren 声明也能解决。

type IProps = {
name: string;
}

解决

原先的写法

import * as React from 'react';

type Props = {
name: string;
};
const Component: React.FC<Props> = ({children}) => {...}

现在的写法

import * as React from 'react';

type Props = {
name: string;
children?: React.ReactNode;
};
const Component: React.FC<Props> = ({children}) => {...}

或者不使用 React.FC 方法

import * as React from 'react';

type Props = {
name: string;
children?: React.ReactNode
};

function Component({children}: Props): React.ReactNode {
...
}