我试图用 create-react-app-typescript 创建一个商店。一切都很顺利.....然而,这个错误发生了“参数‘props’隐式地有一个‘any’类型”,我在互联网上找到了一个解决方案……它建议为 Prop 创建一个接口(interface)。但我真的不知道那个物体有什么,所以我什至不能那样做......
感谢任何帮助! (我是 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 类上使用泛型声明您的 props 和 state 对象。尽可能避免使用 any
关键字。
提示: 现代 IDE 允许您 F12 并检查定义文件,如果您是 TypeScript 的新手,这将是一个很大的帮助。您还可以阅读 DT GitHub 存储库上的 React 定义,React.Component 定义为 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>
);
}
}
关于javascript - 为什么参数 'props' 隐式具有 'any' 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52735288/