Oli*_*n04 7 interface object partial typescript
我有这个打字稿类,需要在构造时提供泛型类型:
type Partial<T> = {
  [P in keyof T]?: T[P];
};
class Foo<Bar> {
  bis: Partial<Bar> = {}; // (1)
  constructor() {
    console.log(typeof this.bis);  // object
    this.bis = {...this.bis};  // (2) Spread types may only be created from object types
  }
}
Run Code Online (Sandbox Code Playgroud)
但是,正如你在上面看到的那样,我在(1)处没有得到错误,但我在(2)处得到错误.
为什么是这样?我该如何解决?
Edit1: 
 
我在Typescript github上打开了一个问题.
<object>解决此问题的方法是使用,<any>或在您的情况下显式转换对象<Bar>。
我不知道你的要求是否允许,但看看 -
type Partial<T> = {
  [P in keyof T]?: T[P];
};
class Foo<Bar> {
  bis: Partial<Bar> = {}; // (1)
  constructor() {
    console.log(typeof this.bis);  // object
    this.bis = {...<Bar>this.bis};  
  }
}
Run Code Online (Sandbox Code Playgroud)