Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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<{}>'."

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;

in Jumbo.tsx:

const Jumbo = () => (
   <FormExample />
);
like image 303
newgrad Avatar asked Mar 07 '18 04:03

newgrad


People also ask

Does not exist on type readonly React State?

The React. js error "Property does not exist on type 'Readonly<{}>'" occurs when we try to access the props or state of a class component which we haven't typed. To solve the error, use the generic on the React. Component class to type the props or state objects of the class.

How do you change the type of a React?

To update your React version, install the latest versions of the react and react-dom packages by running npm install react@latest react-dom@latest . If you use create-react-app , also update the version of react-scripts . Copied! The command will update the versions of the react-related packages.


3 Answers

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> {... }
like image 117
Harish Avatar answered Oct 09 '22 20:10

Harish


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);
    }
}
like image 30
Ozgur Sahin Avatar answered Oct 09 '22 22:10

Ozgur Sahin


I just got this error on a functional component.

In order to get information such as props.children as well as custom props, you should do the following.

import { FunctionComponent } from 'react';

const Layout: FunctionComponent<{ hello: string }> = props => (
  <div style={layoutStyle}>
    <Header />
    {props.hello}
    {props.children}
  </div>
);
like image 11
Stephen Paul Avatar answered Oct 09 '22 22:10

Stephen Paul