TypeScript& React / JSX:在为反应组件的超类提供props时提供tsc编译器错误

时间:2017-06-02 00:22:34

标签: reactjs typescript jsx

重现的最小例子:

const React = require("react");

class ExampleForm extends React.Component {
    constructor(props) {
        super(props);
    }

    render() {
        return <p>{ this.props.msg }</p>;
    }
};

编译器命令:

tsc --jsx react react_components/example.tsx

错误:

  

react_components / example.tsx(5,9):错误TS2346:提供的参数与呼叫目标的任何签名都不匹配。

这是一个错误吗?我错过了一些tsc命令行标志/选项吗?

编辑:

安装类型:

  • “@ types / react”:“^ 15.0.25”
  • “@ types / node”:“^ 7.0.23”

tsc版本是2.3.4

1 个答案:

答案 0 :(得分:2)

尝试使用es6导入代替React,并确保将delcare用于道具。

e.g。

import * as React from 'react';

interface IProps {
  msg: string;
}

export class ExampleForm extends React.Component<IProps, {}> {
  constructor(props: IProps) {
    super(props);
  }

  render() {
    return <p>{this.props.msg}</p>;
  }
}