Last updated: Feb 29, 2024
Reading timeยท3 min
The React.js error "Parameter 'props' implicitly has an 'any' type" occurs when we don't type the props of a function or class component or forget to install typings for React.
To solve the error explicitly set a type for the props
object in your
components.
First, make sure you have installed the typings for React. Open your terminal in
your project's root directory (where your package.json
file is) and run the
following command.
# ๐๏ธ with NPM npm install --save-dev @types/react @types/react-dom # ---------------------------------------------- # ๐๏ธ with YARN yarn add @types/react @types/react-dom --dev
Try restarting your IDE and development server.
props
of a componentIf that doesn't help, chances are you have forgotten to explicitly type the
props
of a function or class component.
// โ๏ธ 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="James" age={30} country="Australia" /> </div> ); } export default App;
The issue is that we haven't set the type for the props
in the function
component.
To solve the error, we have to explicitly type the props
parameter.
interface PersonProps { name: string; age: number; country: string; children?: React.ReactNode; // ๐๏ธ for demo purposes } 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="James" age={30} country="Australia" /> </div> ); } export default App;
We defined an interface to type the component's props explicitly, which solves the error.
children
prop, but you would have to do that if you pass children to your component.If you forget to type an event
object, you'll get the
Parameter 'event' implicitly has 'any' type
error.
any
typeIf you don't want to have to explicitly type the props object for your
component, you can use the any
type.
// ๐๏ธ set type to 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="James" age={30} country="Australia" /> </div> ); } export default App;
The any
type
effectively turns off type checking,
so we are able to pass props to the component and access properties on the
object without getting any type-checking errors.
If you have a class component, use a generic 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 props and the state of the component.
If you don't want to have to explicitly type your component's props and state,
you can use the any
type.
import React from 'react'; // ๐๏ธ type checking disabled for props and 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 any
type when typing the props
and state
objects, which
effectively turns off type-checking.
this.props
and this.state
objects without getting a type-checking error.If the error is not resolved, try to delete your node_modules
and
package-lock.json (not
package.json
) files, re-run npm install
and restart your IDE.
If you are on macOS or Linux, issue the following commands in bash
or zsh
.
# for macOS and Linux rm -rf node_modules rm -f package-lock.json rm -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
If you are on Windows, issue the following commands in CMD.
# for Windows rd /s /q "node_modules" del package-lock.json del -f yarn.lock # ๐๏ธ clean npm cache npm cache clean --force # ๐๏ธ install packages npm install
Make sure to restart your IDE and dev server if the error persists. VSCode often glitches and a reboot solves things sometimes.