反应/打字稿:参数'props'隐式具有'any'类型。使用IProps接口后



我不明白为什么我有这种类型的错误

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

因为我之前使用迭代面在构造函数方法中使用道具。 下面我把整个反应组件当我遇到问题时

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;

有人有一些想法吗?

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

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

最新更新