Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ReactJs/Typescript:如何扩展状态接口_Javascript_Reactjs_Typescript - Fatal编程技术网

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