Javascript ReactJs/Typescript:如何扩展状态接口
我有以下资料:Javascript ReactJs/Typescript:如何扩展状态接口,javascript,reactjs,typescript,Javascript,Reactjs,Typescript,我有以下资料: interface EditViewState<T> { entity?: T; } abstract class EditView<T, P, S> extends React.Component<P, EditViewState<T> & S> { constructor(props: P, ctx: any) { super(props, ctx); this.state = {
interface EditViewState<T> {
entity?: T;
}
abstract class EditView<T, P, S> extends React.Component<P, EditViewState<T> & S> {
constructor(props: P, ctx: any) {
super(props, ctx);
this.state = {
mode: "loading" // Type '{ entity: null; }' is not assignable to type 'Readonly<EditViewState<T> & S>'.ts(2322
}
}
componentDidMount() {
this.setState({ mode: "show" }) // Type '"show"' is not assignable to type '("loading" & S["mode"]) | ("show" & S["mode"]) | ("edit" & S["mode"])'.ts(2345)
}
}
接口编辑视图状态{
实体?:T;
}
抽象类EditView扩展了React.Component{
构造器(道具:P,ctx:任意){
超级(道具,ctx);
此.state={
模式:“加载”//Type“{entity:null;}”不可分配给类型“Readonly”.ts(2322
}
}
componentDidMount(){
this.setState({mode:“show”})//类型“show”不可分配给类型“(“加载”&S[“mode”])|”(“显示”&S[“mode”]))|”(“编辑”&S[“mode”])”。ts(2345)
}
}
如何使用
EditViewState
并使用来自具体类的S
对其进行扩展?现在我有了一个使用Exclude
abstract class EditView<T, P, S> extends React.Component<P, Exclude<EditViewState<T> & S, keyof S>> {
constructor(props: P, ctx: any) {
super(props, ctx);
//@ts-ignore https://github.com/microsoft/TypeScript/issues/38947
this.state = {
mode: "loading"
}
}
componentDidMount() {
this.setState({ mode: "show" })
}
}
抽象类EditView扩展了React.Component{
构造器(道具:P,ctx:任意){
超级(道具,ctx);
//@ts忽略https://github.com/microsoft/TypeScript/issues/38947
此.state={
模式:“加载”
}
}
componentDidMount(){
this.setState({mode:“show”})
}
}
现在,Conrecte类可以自动完成EditViewState
和它自己的状态s