为什么参数“ props”隐式具有“ any”类型?

Eva*_*ows 4 javascript typescript redux create-react-app

我试图用create-react-app-typescript创建一家商店。一切都很好.....但是,此错误发生了,“参数'props'隐式具有'any'类型”,我在互联网上找到了解决方案...建议为props创建一个接口。但是我真的不知道那个物体有什么,所以我什至不能做...

任何帮助表示赞赏!(我是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);
Run Code Online (Sandbox Code Playgroud)

Per*_*son 6

您应该始终在Component类上使用泛型声明props和state对象。any尽可能避免使用关键字。

提示:现代化的IDE允许您F12并检查定义文件,如果您是TypeScript的新手,那将是一个很大的帮助。您还可以在DT GitHub存储库上阅读React定义,此处定义了React.Component

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>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)