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

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

问题描述

我试图用 create-react-app-typescript 创建一个商店.一切都很顺利.....但是然后发生了这个错误参数'道具'隐式具有'任何'类型",我在互联网上找到了一个解决方案......它建议为道具创建一个接口.但我真的不知道那个对象有什么所以我什至不能这样做......

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

import * as React from 'react';从react-redux"导入{连接};从./utilities/mapStateToProp"导入 mapStateToProps;类 App 扩展了 React.Component {构造函数(道具){超级(道具);}公共渲染(){返回 (<div className="应用程序"><h1>Go 类型反应</h1>

);}}导出默认连接(mapStateToProps)(应用程序);

解决方案

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

提示:现代 IDE 允许您按 F12 并检查定义文件,如果您不熟悉 TypeScript,这将有很大帮助.你也可以在 DT GitHub repo 上阅读 React 定义,React.Component 被定义为 这里

type AppProps = {}输入 AppState = {}类 App 扩展了 React.Component{构造函数(道具:AppProps){超级(道具);//this.props 已经是 AppProps 类型.//只有构造函数 props 是 any}公共渲染(){返回 (<div className="应用程序"><h1>Go 类型反应</h1>

);}}

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

解决方案

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

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

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆