【发布时间】:2019-07-06 08:22:33
【问题描述】:
使用 @types/react 16.8.2 和 TypeScript 3.3.1。
我直接从React documentation 中提取了这个前向引用示例并添加了几个类型参数:
const FancyButton = React.forwardRef<HTMLButtonElement>((props, ref) => (
<button ref={ref} className="FancyButton">
{props.children}
</button>
));
// You can now get a ref directly to the DOM button:
const ref = React.createRef<HTMLButtonElement>();
<FancyButton ref={ref}>Click me!</FancyButton>;
FancyButton 下的最后一行出现以下错误:
类型“
{ children: string; ref: RefObject<HTMLButtonElement>; }”不是 可分配给类型“IntrinsicAttributes & RefAttributes<HTMLButtonElement>”。属性“children”不 存在于类型'IntrinsicAttributes & RefAttributes<HTMLButtonElement>'.ts(2322)
React.forwardRef 的返回值的类型定义似乎是错误的,没有正确合并 children 属性。如果我让<FancyButton> 自动关闭,错误就会消失。缺少此错误的搜索结果让我相信我遗漏了一些明显的东西。
【问题讨论】:
标签: reactjs typescript jsx tsx