我是打字稿的新手,这个问题使我感到困惑。 这是我的代码。
import { IProps, IState } from './types';
class Profile extends Component<RouteComponentProps & IProps, IState>{
state: IState = {
form: {
first_name: this.props.user.first_name,
last_name : this.props.user.last_name,
email : this.props.user.email,
phone : this.props.user.phone || ''
},
isLoading: false
};
validator = new SimpleReactValidator({autoForceUpdate: true});
render () {
const { user } = this.props;
const { form, isLoading } = this.state;
return (<div></div>)
}
}
export default Profile;
这些是我的类型:
export interface IState {
form: {
[key: string]: any;
};
isLoading: boolean;
}
export interface IProps {
children?: Element;
user : any;
}
这是我的index.ts:
import Profile from './pages/profile/Profile';
const mapStateToProps = (state: IAppState) => {
return {
user: state.Auth.user
};
};
export default connect(mapStateToProps)(Profile);
我的代码编辑器将(Profile)突出显示为错误。
我的终端抛出以下错误:
'typeof Profile'类型的参数不能分配给>'ComponentType'类型的参数。 不能将“ typeof Profile”类型分配给“ ComponentClassany>”类型。
我想知道是否可以在此处进行代码更改以修复此Typescript“错误”。
答案 0 :(得分:0)
import { IProps, IState } from './types';
import {connect} from "react-redux"
class Profile extends Component<RouteComponentProps & IProps, IState>{
state: IState = {
form: {
first_name: this.props.user.first_name,
last_name : this.props.user.last_name,
email : this.props.user.email,
phone : this.props.user.phone || ''
},
isLoading: false
};
validator = new SimpleReactValidator({autoForceUpdate: true});
render () {
const { user } = this.props;
const { form, isLoading } = this.state;
return (<div></div>)
}
}
const mapStateToProps = (state: IAppState) => {
return {
user: state.Auth.user
};
};
export default connect(mapStateToProps)(Profile);