I was trying to create a store with create-react-app-typescript. Everything was going fine.....however then this error occurred "Parameter 'props' implicitly has an 'any' type" and I found a solution for this in the internet...it suggested to create a interface for props. But I really don't know what that object has so i can't even do that...
Any help is appreciated! (I am new to TypeScript)
import * as React from 'react';
import { connect } from "react-redux";
import mapStateToProps from "./utilities/mapStateToProp";
class App extends React.Component {
constructor(props){
super(props);
}
public render() {
return (
<div className="App">
<h1>Go Type React</h1>
</div>
);
}
}
export default connect(mapStateToProps)(App);
I was trying to create a store with create-react-app-typescript. Everything was going fine.....however then this error occurred "Parameter 'props' implicitly has an 'any' type" and I found a solution for this in the internet...it suggested to create a interface for props. But I really don't know what that object has so i can't even do that...
Any help is appreciated! (I am new to TypeScript)
import * as React from 'react';
import { connect } from "react-redux";
import mapStateToProps from "./utilities/mapStateToProp";
class App extends React.Component {
constructor(props){
super(props);
}
public render() {
return (
<div className="App">
<h1>Go Type React</h1>
</div>
);
}
}
export default connect(mapStateToProps)(App);
Share
Improve this question
edited Oct 10, 2018 at 7:59
Constantin Chirila
2,1192 gold badges20 silver badges33 bronze badges
asked Oct 10, 2018 at 7:54
Evading ShadowsEvading Shadows
4891 gold badge6 silver badges24 bronze badges
5
|
2 Answers
Reset to default 11You should always declare your props and state objects with generics on the Component class. Restrain from using the any
keyword whenever possible.
Tip: A modern IDE allows you to F12 and inspect the definition file, that is a great help if your new to TypeScript. You can also read the React definition on the DT GitHub repo, the React.Component are defined here
type AppProps = {}
type AppState = {}
class App extends React.Component<AppProps, AppState> {
constructor(props: AppProps) {
super(props);
//this.props will already be of type AppProps.
//Only the constructor props are any
}
public render() {
return (
<div className="App">
<h1>Go Type React</h1>
</div>
);
}
}
For function components:
import { FunctionComponent } from "react";
// This allows you to use `props.children`
export const App: FunctionComponent = (props) => {}
// Or if you have custom props you can define them
export const App: FunctionComponent<{key: String}> = (props) => {}
props: any
. Or enable implicitany
in your TS config. – Sergiu Paraschiv Commented Oct 10, 2018 at 7:56constructor(props: any){
– Constantin Chirila Commented Oct 10, 2018 at 7:57