代码之家  ›  专栏  ›  技术社区  ›  Jamie Treworgy

TypeScript 3.0 React defaultProps支持不工作

  •  0
  • Jamie Treworgy  · 技术社区  · 7 年前

    尝试使用自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]类型“{}”不可分配给类型“Readonly”。所有物

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

    人们是否成功地使用了此功能?是否有编译器指令或我需要启用的东西?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jamie Treworgy    7 年前

    尝试升级到的较新版本 @types/react ,它适用于16.4.11。

    推荐文章