Reactjs,Typescript - 在打字稿中铸造道具 - React Component

时间:2017-06-06 20:53:50

标签: reactjs typescript

我正在使用带有React的typescript 2.3.4。我收到错误TS7006:参数'props'隐含有'any'类型。什么是在打字稿中投放道具的正确方法?

感谢任何帮助。

var notAuthorized = false;
for (let i = 0; i < ln; i++) {
  let r = cdtArray[i];
  if(AuthService.hasPersona(r)){
    notAuthorized = true; //A Persona is authorized
    return;
  }
}

if (notAuthorized) {
    angular.element(elem).remove();
}

1 个答案:

答案 0 :(得分:6)

您收到此错误,因为构造函数的参数没有类型注释。它应该是:

constructor(props: Props) {
    super(props);
    this.state = {
       name: 'baz111'
    };
}

当函数参数没有类型注释时,它将隐式地键入any。如果启用了tsconfig.json文件中的noImplicitAny选项,则在发生此类情况(您的情况)时将导致错误。

另一方面,您不必重新声明州和道具领域,您可以将它们排除在外。

相关问题