最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does parameter 'props' implicitly has an 'any' type? - Stack Overflow

programmeradmin2浏览0评论

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
  • If you don't know then tell TS this. props: any. Or enable implicit any in your TS config. – Sergiu Paraschiv Commented Oct 10, 2018 at 7:56
  • 2 Try this: constructor(props: any){ – Constantin Chirila Commented Oct 10, 2018 at 7:57
  • Seems like here you can find the example: charleslbryant.gitbooks.io/hello-react-and-typescript/content/… – Sergii Vorobei Commented Oct 10, 2018 at 8:00
  • Thanks for your help! Anyone answer the question and I will approve it – Evading Shadows Commented Oct 10, 2018 at 8:03
  • Does this answer your question? react/typescript: Parameter 'props' implicitly has an 'any' type error – thisismydesign Commented Jul 25, 2020 at 1:50
Add a comment  | 

2 Answers 2

Reset to default 11

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

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) => {}
发布评论

评论列表(0)

  1. 暂无评论