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
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
How to solve the "Function components cannot have string refs" error in React
Publish Date:2025/03/15 Views:138 Category:React
-
When we use a string as a reference in a function component, we get the error "Function components cannot have string refs". To fix this error, we need to use the useRef() hook to get a mutable ref object that we can use as a component.
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
Solve React's Unexpected default export of anonymous function error
Publish Date:2025/03/15 Views:120 Category:React
-
When we try to export an anonymous function using default export, it results in the warning “Unexpected default export of anonymous function.” To fix this error, you need to give the function a name before exporting it.
React error Type '() => JSX.Element[]' is not assignable to type FunctionCompo
Publish Date:2025/03/15 Views:51 Category:React
-
When we try to return an array of elements from a function component, a React.js error “Type '() => JSX.Element[]' is not assignable to type FunctionComponent” occurs. To fix the error, you need to wrap the array of elements in a React fragment.
How to solve React Type {children: Element} has no properties in common with type
Publish Date:2025/03/15 Views:103 Category:React
-
React.js error "Type {children: Element} has no properties in common with type IntrinsicAttributes" occurs when we try to pass children prop to a component without any props. To resolve the error, you need to define and type the properties on the comp
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