Reactjs,Typescript-在Typescript中投射道具-React组件
我正在使用带有React的typescript 2.3.4。我得到错误TS7006:参数'props'隐式地具有'any'类型。用打字机投射道具的正确方法是什么 感谢您的帮助Reactjs,Typescript-在Typescript中投射道具-React组件,reactjs,typescript,Reactjs,Typescript,我正在使用带有React的typescript 2.3.4。我得到错误TS7006:参数'props'隐式地具有'any'类型。用打字机投射道具的正确方法是什么 感谢您的帮助 interface State { name: string; } interface Props { } export class Profile extends React.Component<Props, State> { public state: State; publ
interface State {
name: string;
}
interface Props {
}
export class Profile extends React.Component<Props, State> {
public state: State;
public props: Props;
constructor(props){
super(props);
this.state = {
name: 'baz111'
};
}
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>baz</div>
</section>
<section>
<h3>profile 2</h3>
<div>{this.state.name}</div>
</section>
</section>
)
}
}
接口状态{
名称:字符串;
}
界面道具{
}
导出类配置文件扩展了React.Component{
公共国家:国家;
公共道具:道具;
建造师(道具){
超级(道具);
此.state={
名称:“baz111”
};
}
公共渲染(){
返回(
概况1
巴兹
概况2
{this.state.name}
)
}
}
您收到此错误是因为构造函数的参数没有类型注释。应该是:
constructor(props: Props) {
super(props);
this.state = {
name: 'baz111'
};
}
当函数参数没有类型注释时,它将隐式地被键入any
。如果在tsconfig.json文件中启用了noImplicitAny
选项,则在发生这种情况(您的情况)时会导致错误
另一方面,您不必重新声明state和props字段,只需将它们忽略即可