【发布时间】:2018-11-28 09:20:49
【问题描述】:
我正在使用 TypeScript 3.0 并根据来自 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html 的文档
使用
static defaultProps: Pick<Props, "name">;作为显式类型 代替注释,或者不添加类型注释,如 上面的例子。
但是,如果我写:
import React, { Component } from 'react';
interface Props {
counter: number;
}
export default class NumberCounter extends Component<Props> {
static defaultProps = {
counter: "i am not a number"
};
render() {
return (
<div>{this.props.counter}</div>
);
}
}
它可以编译并且可以工作,显然 prop 的默认值没有经过类型检查。尽管如此,当我尝试错误地使用该组件时:
<NumberCounter
counter="still not a number"
/>
它显示错误,并且在其他地方使用时会正确进行类型检查。这是 TypeScript 的已知错误吗?
【问题讨论】:
标签: reactjs typescript react-props