TypeScript 3.0 React defaultProps支持不起作用

时间:2019-01-24 16:58:54

标签: typescript

尝试使用TypeScript 3.0以来提供的此功能:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#support-for-defaultprops-in-jsx

export interface Props {
  name: string;
}

export class Greet extends React.Component<Props> {
  static defaultProps = { name: 'world'};
  render() {
      const { name } = this.props;
      return <div>Hello ${name.toUpperCase()}!</div>;
  }
}

// Type-checks! No type assertions needed!
let el = <Greet />

根据上面的文档,这应该通过类型检查。这是文档中的确切代码,但是我在行let el = <Greet />

上收到此类型检查错误。
  

[ts]类型“ {}”不可分配给类型“只读”。属性   类型“ {}”中缺少“名称”。 [2322]

如果该功能不存在,这就是我的期望。尝试使用TypeScript 3.1.6和3.2.4。

人们成功使用了此功能吗?是否有编译器指令或需要启用的内容?

1 个答案:

答案 0 :(得分:1)

尝试升级到@types/react的较新版本,它可与16.4.11一起使用。

相关问题