reactjs 反应/打字稿:参数“道具”隐式具有“任何”类型错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49144169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
react/typescript: Parameter 'props' implicitly has an 'any' type error
提问by newgrad
When I try this sample code from react-bootstrap, I keep getting errors such as " Parameter 'context' implicitly has an 'any' type; "Property 'value' does not exist on type 'Readonly<{}>'."
当我从 react-bootstrap 尝试此示例代码时,我不断收到错误,例如“参数 'context' 隐式具有 'any' 类型;“属性 'value' 不存在于类型 'Readonly<{}>'。”
in form.tsx:
在 form.tsx 中:
class FormExample extends React.Component {
constructor(props, context) {
super(props, context);
this.handleChange = this.handleChange.bind(this);
this.state = {
value: ''
};
}
getValidationState() {
const length = this.state.value.length;
if (length > 10) return 'success';
else if (length > 5) return 'warning';
else if (length > 0) return 'error';
return null;
}
handleChange(e) {
this.setState({ value: e.target.value });
}
render() {
return (
<form>
<FormGroup
controlId="formBasicText"
validationState={this.getValidationState()}
>
<ControlLabel>Working example with validation</ControlLabel>
<FormControl
type="text"
value={this.state.value}
placeholder="Enter text"
onChange={this.handleChange}
/>
<FormControl.Feedback />
<HelpBlock>Validation is based on string length.</HelpBlock>
</FormGroup>
</form>
);
}
}
export default FormExample;
in Jumbo.tsx:
在 Jumbo.tsx 中:
const Jumbo = () => (
<FormExample />
);
回答by Harish
In typeScript you should install @types/reactand while extending the React.Component you need to specify the props and state types. Here is the example
在 typeScript 中你应该安装@types/react并且在扩展 React.Component 时你需要指定 props 和 state 类型。这是例子
import * as React from 'react'
interface Props {
... // your props validation
}
interface State {
... // state types
}
class FormExample extends React.Component<Props, State> {... }
回答by Ozgur Sahin
Specifying the type of the constructor parameter resolved this issue in my case.
在我的情况下,指定构造函数参数的类型解决了这个问题。
class Clock extends React.Component<any, any> {
constructor(props: any) {
super(props);
}
}
回答by Sujit.Warrier
in type script you need to specify the type of props you are going to send or it takes the default type defined tin @types/react. if you dont want to specify any type then explicitly ask the component to expect state and props of 'any' type.
在类型脚本中,您需要指定要发送的道具类型,或者它采用默认类型定义 tin @types/react。如果您不想指定任何类型,则明确要求组件期望“任何”类型的状态和道具。
class FormExample extends React.Component<any,any> {
the first type argument is for defining the type of props you are expecting , the other is for type of state of the component.
第一个类型参数用于定义您期望的道具类型,另一个用于组件的状态类型。
回答by Stephen Paul
I just got this error on a functional component.
我刚刚在功能组件上收到此错误。
In order to get information such as props.childrenas well as custom props, you should do the following.
为了获取诸如props.children自定义道具之类的信息,您应该执行以下操作。
import { FunctionComponent } from 'react';
const Layout: FunctionComponent<{ hello: string }> = props => (
<div style={layoutStyle}>
<Header />
{props.hello}
{props.children}
</div>
);

