react / typescript:参数'道具'隐含地有一个“任何”的类型。使用IProps界面后

时间:2018-06-13 10:15:59

标签: reactjs typescript redux react-redux

我不明白为什么会出现这种错误

Parameter 'props' implicitly has an 'any' type.

因为我之前使用iterface在构造函数方法中使用了props。 当我遇到问题时,我将完整的反应组件放在下面



import { connect } from 'react-redux';
import { FetchPokemon } from '../../actions';

import * as React from 'react';
import { HeaderComponent } from './HeaderComponent';

import {IStoreState} from '../../types';

interface IProps {
  pokemon: object[];
  FetchPokemon: () => void;
}
interface IState {
  example: boolean
}

class HeaderContainer extends React.Component<IProps, IState> {
  constructor(props) {
    super(props);

    this.state = {
      example: false
    }

    this.FetchAction = this.FetchAction.bind(this);
  }

  public FetchAction = () => {
    this.props.FetchPokemon()
  }

  public render() {
    return (
      <div>
        {console.log(this.props)}
        <HeaderComponent fetchAction={this.FetchAction}/>
      </div>
    )
  }
}

const mapStateToProps = (state: IStoreState) => ({
  pokemon: state.fetchPokemon
})

export default connect(mapStateToProps, {FetchPokemon})(HeaderContainer) as any;
&#13;
&#13;
&#13;

有人有一些想法吗?

1 个答案:

答案 0 :(得分:1)

TypeScript的严格性要求您清楚某些类型和对象,具体取决于您的转换器配置。当谈到React和TypeScript时,建议按如下方式编写构造函数:

class Component extends React.Component<Props, State> {
    public constructor(props: Props) {
        super(props as any);
    }
}