VSD*_*kar 7 typescript react-jsx
我的TypeScript版本是 2.3.2
根据这个类型检查JSX的孩子
以下代码应该有效:
import * as React from 'react';
import * as ReactDOM from 'react-dom';
interface TestProps {
children: string | JSX.Element;
}
const Foo = (props: TestProps) => <div>{props.children}</div>;
// Error on Foo
ReactDOM.render(
<Foo>
<div>Test</div>
</Foo>,
document.getElementById('content'),
);
Run Code Online (Sandbox Code Playgroud)
但是我得到以下编译错误:
TestTsxChildren> tsc --version
Version 2.3.2
TestTsxChildren> tsc
main.tsx(11,5): error TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & TestProps'.
Type '{}' is not assignable to type 'TestProps'.
Property 'children' is missing in type '{}'.
Run Code Online (Sandbox Code Playgroud)
我做错了什么?或者我不明白这个问题试图修复什么?
小智 1
这是因为 React 的定义文件没有更新。
定义文件需要包含在内interface ElementChildrenAttribute { children: {}; },以便 TypeScript 编译器知道“children”属性的名称。
PR 更新定义文件:https://github.com/DefinitelyTyped/DefinitelyTyped/pull/16327)
| 归档时间: |
|
| 查看次数: |
1763 次 |
| 最近记录: |