我试图用create-react-app-typescript创建一家商店。一切都很好.....但是,此错误发生了,“参数'props'隐式具有'any'类型”,我在互联网上找到了解决方案...建议为props创建一个接口。但是我真的不知道那个物体有什么,所以我什至不能做...
感谢您的帮助! (我是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);
答案 0 :(得分:1)
您应该始终在Component类上使用泛型声明道具和状态对象。尽可能避免使用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>
);
}
}
答案 1 :(得分:1)
对于功能组件:
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) => {}