反应/打字稿:参数“道具"隐式具有“任何"类型错误 [英] react/typescript: Parameter 'props' implicitly has an 'any' type error
本文介绍了反应/打字稿:参数“道具"隐式具有“任何"类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我从 react-bootstrap 尝试此示例代码时,我不断收到错误消息,例如参数‘上下文’隐式具有‘任何’类型;‘属性‘值’不存在于‘只读<{}>’类型上."
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<{}>'."
在 form.tsx 中:
in 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;
在 Jumbo.tsx 中:
in Jumbo.tsx:
const Jumbo = () => (
<FormExample />
);
推荐答案
在 typeScript 中,您应该安装 @types/react 并且在扩展 React.Component
时,您需要指定 props
和 state
> 类型.这是例子
In typeScript you should install @types/react and while extending the React.Component
you need to specify the props
and state
types.
Here is the example
import * as React from 'react'
interface Props {
... // your props validation
}
interface State {
... // state types
}
class FormExample extends React.Component<Props, State> {... }
这篇关于反应/打字稿:参数“道具"隐式具有“任何"类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文