Fix Parameter 'props' implicitly has 'any' type error in React
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 props
the type of the object in the component.
The first thing we should make sure is that we have React typings installed. package.json
Open 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 props
the 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 props
the object for our component, we can use any
the 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;
any
The type effectively turns off type checking, so we are able to props
pass 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 any
types.
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 install
and 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.
Related Articles
React Tutorial - Transferring Props
Publish Date:2025/03/16 Views:188 Category:React
-
React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.
React Tutorial: Props Anti-Pattern
Publish Date:2025/03/16 Views:187 Category:React
-
React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.
React Tutorial - Props Validation
Publish Date:2025/03/16 Views:102 Category:React
-
Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.
React tutorial: Types of Props for child components
Publish Date:2025/03/16 Views:172 Category:React
-
Usually, the child components of a React component are a group, that is, the child components are an array. Introduction to Type of the Children Props.
How to solve the error Uncaught TypeError: Cannot read properties of undefined in
Publish Date:2025/03/16 Views:153 Category:React
-
In the process of React development, we often encounter some errors. Here we look at an error reported in App.js. The error is as follows: App.js:69 Uncaught TypeError: Cannot read properties of undefined (reading 'setState') at onInput
Why do you need to bind event handlers in React Class Components?
Publish Date:2025/03/16 Views:60 Category:React
-
When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:
Solution to the error "does not contain a default export" in React
Publish Date:2025/03/16 Views:191 Category:React
-
When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.
Error in React: Attempted import error 'X' is not exported from Solution
Publish Date:2025/03/16 Views:78 Category:React
-
In React, the error “Attempted import error 'X' is not exported from” in React.js occurs when we try to import a named import that does not exist in the specified file. To fix the error, make sure the module has named exports and you have not obfu
Solve the Module not found: Can't resolve 'react-bootstrap' error
Publish Date:2025/03/16 Views:90 Category:React
-
To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap