首页 > 解决方案 > Typescript / React:带有函数语句的类型中缺少属性'children'

问题描述

我有一个带孩子的 React 组件。

收到打字稿错误:

Property 'children' is missing in type ... but required in type 'FixedWidthLayout'

该组件的定义类似于函数语句。

export default function FixedWidthLayout { ... }

我不想写它喜欢和表达..

IE

export const FixedWidthLayout = () => {...}

因此,做类似...

const FixedWidthLayout: React.FC<Props> = () => {...}

..不是一种选择。

如何在带有函数语句的 Typescript 中使用它?

标签: reactjstypescript

解决方案


你在哪里得到错误?

这应该只是工作:

import * as React from 'react';

interface Props {
  children: React.Node;
}

export default function FixedWidthLayout(props: Props): JSX.Element {
  /* ... */
}

<FixedWidthLayout>Hello</FixedWidthLayout>

推荐阅读