Why does parameter 'props' implicitly has

2020-06-22 02:31发布

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);

1条回答
萌系小妹纸
2楼-- · 2020-06-22 03:13

You 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>
        );
    }
}
查看更多
登录 后发表回答