JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

Fix Parameter 'props' implicitly has 'any' type error in React

Author:JIYIK Last Updated:2025/03/15 Views:

When we don’t type the properties of a function or class component or forget to install typings for React, React.js will give an error “Parameter 'props' implicitly has an 'any' type”. To fix the error, explicitly set propsthe type of the object in the component.

React Parameter 'props' implicitly has 'any' type error

The first thing we should make sure is that we have React typings installed. package.jsonOpen a terminal in the root directory of your project (where your files are located) and run the following command.

# 👇️ 使用 NPM
$ npm install --save-dev @types/react @types/react-dom

# ----------------------------------------------

# 👇️ 使用 YARN
$ yarn add @types/react @types/react-dom --dev

Try restarting the IDE and the service.

If that doesn't help, it's possible that you forgot to explicitly type a function or class component props. Here's sample code that produces the above error.

// ⛔️ Parameter 'props' implicitly has an 'any' type.ts(7006)
function Person(props) {
  return (
    <div>
      <h2>{props.name}</h2>
      <h2>{props.age}</h2>
      <h2>{props.country}</h2>
    </div>
  );
}

function App() {
  return (
    <div>
      <Person name="jiyik.com" age={30} country="China" />
    </div>
  );
}

export default App;

The problem with the above code snippet is that we haven't set a type for the function component props.

To fix this error, we have to explicitly enter propsthe parameters.

interface PersonProps {
  name: string;
  age: number;
  country: string;
  children?: React.ReactNode;
}

function Person(props: PersonProps) {
  return (
    <div>
      <h2>{props.name}</h2>
      <h2>{props.age}</h2>
      <h2>{props.country}</h2>
    </div>
  );
}

function App() {
  return (
    <div>
      <Person name="jiyik.com" age={30} country="China" />
    </div>
  );
}

export default App;

We defined an interface to explicitly input the props of the component, which resolved the error.

We don't have to set the children property, but we would have to do so if we passed children to a component.

If we don't want to explicitly type propsthe object for our component, we can use anythe type.

// 👇️ 设置为 any 类型
function Person(props: any) {
  return (
    <div>
      <h2>{props.name}</h2>
      <h2>{props.age}</h2>
      <h2>{props.country}</h2>
    </div>
  );
}

function App() {
  return (
    <div>
      <Person name="jiyik.com" age={30} country="China" />
    </div>
  );
}

export default App;

anyThe type effectively turns off type checking, so we are able to propspass the to the component and access properties on the object without getting any type checking errors.

If we have a class component, use generics to type its props and state.

import React from 'react';

interface PersonProps {
  name: string;
  age: number;
  country: string;
  children?: React.ReactNode;
}


interface PersonState {
  value: string;
}

// 👇️ React.Component<PropsType, StateType>
class Person extends React.Component<PersonProps, PersonState> {
  render() {
    return (
      <div>
        <h2>{this.props.name}</h2>
        <h2>{this.props.age}</h2>
        <h2>{this.props.country}</h2>
      </div>
    );
  }
}

export default function App() {
  return (
    <div>
      <Person name="James" age={30} country="Australia" />
    </div>
  );
}

We explicitly provided types for the component's props and state, thus resolving the error.

If you don't want to explicitly type your component's props and state, you can use anytypes.

import React from 'react';

// 👇️ 对 props 和 state 禁用类型检查
class App extends React.Component<any, any> {
  constructor(props: any) {
    super(props);

    this.state = {value: ''};
  }

  handleChange = (event: any) => {
    this.setState({value: event.target.value});
  };

  render() {
    return (
      <div>
        <form>
          <input
            onChange={this.handleChange}
            type="text"
            value={this.state.value}
          />
          <button type="submit">Submit</button>
        </form>
      </div>
    );
  }
}

export default App;

We used the type when typing the props and state objects any, which effectively turned off type checking.

Now we can access any property on the this.props and this.state objects without getting type checking errors.

If the error is not resolved, try deleting the node_modules and package-lock.json (not package.json) files, re-running npm installand restarting the IDE.

# 👇️ 删除 node_modules 和 package-lock.json
$ rm -rf node_modules
$ rm -f package-lock.json

# 👇️ 清除 npm 缓存
$ npm cache clean --force

$ npm install

If the error persists, make sure to restart the IDE and the development server. VSCode often crashes and sometimes restarting can fix the problem.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Fix Uncaught ReferenceError: useState is not defined in React

Publish Date:2025/03/15 Views:142 Category:React

When we use the useState hook in our code but forget to import it, it generates the error Uncaught ReferenceError: useState is not defined. To fix this error, you need to import the hook before using it import {useState} from react . // ?️

React error Uncaught ReferenceError: process is not defined solution

Publish Date:2025/03/15 Views:116 Category:React

To resolve the “Uncaught ReferenceError: process is not defined” error in React, open a terminal in the root directory of your project and update the version of the `react-scripts` package by running `npm install react-scripts@latest` and reinstal

React Error Property 'X' does not exist on type 'Readonly<{}>'

Publish Date:2025/03/15 Views:139 Category:React

The React.js error “Property does not exist on type 'Readonly'” occurs when we try to access the props or state of an untyped class component. To fix the error, you need to use generics on the React.Component class to type the props or state of th

Fix the value prop on input should not be null error in React

Publish Date:2025/03/15 Views:141 Category:React

The warning "value prop on input should not be null" is caused when we set the initial value of an input to null or override the initial value setting it to null, e.g. from an empty API response. Use a fallback value to solve this problem.

Property does not exist on type 'JSX.IntrinsicElements' error in React

Publish Date:2025/03/15 Views:188 Category:React

When the name of a component starts with a lowercase letter, an error "Property does not exist on type 'JSX.IntrinsicElements" will appear. To fix this error, you need to make sure you always start your component names with an uppercase letter, instal

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial