小编典典

react / typescript:参数“ props”隐式具有“ any”类型错误

reactjs

当我从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 />
);

阅读 1860

收藏
2020-07-22

共1个答案

小编典典

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

import * as React from 'react'

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

interface State {
  ... // state types
}

class FormExample extends React.Component<Props, State> {... }
2020-07-22