gpt4 book ai didi

twitter-bootstrap - react / typescript : Parameter 'props' implicitly has an 'any' type error

转载 作者:搜寻专家 更新时间:2023-10-30 20:41:23 24 4
gpt4 key购买 nike

当我从 react-bootstrap 尝试此示例代码时,我不断收到错误消息,例如“参数‘context’隐式具有‘any’类型;“属性‘value’在类型‘Readonly<{}>’上不存在”

在 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 中:

const Jumbo = () => (
<FormExample />
);

最佳答案

typeScript 中,您应该安装 @types/react在扩展 React.Component 时,您需要指定 propsstate 类型。这是例子

import * as React from 'react'

interface Props {
... // your props validation
}

interface State {
... // state types
}

class FormExample extends React.Component<Props, State> {... }

关于twitter-bootstrap - react / typescript : Parameter 'props' implicitly has an 'any' type error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49144169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com