为什么参数“ props”隐式具有“ any”类型? [英] Why does parameter 'props' implicitly has an 'any' type?

查看:2778
本文介绍了为什么参数“ props”隐式具有“ any”类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

任何帮助都值得感谢! (我是TypeScript的新手)

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


推荐答案

您应始终使用泛型声明道具和状态对象在Component类上。尽可能避免使用 any 关键字。

You should always declare your props and state objects with generics on the Component class. Restrain from using the any keyword whenever possible.

提示:
现代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>
        );
    }
}

这篇关于为什么参数“ props”隐式具有“ any”类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆