Unexpected error "Spread types can only be generated from object types" when using generics

I have this typescript class that requires the generic type to be provided when constructing:

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
  }
}

      

Anyway, as you can see above, I don't get an error at (1), but I am doing it at (2).
Why is this? And how to fix it?

Edit 1:
I have opened an issue on github typescript.

+3


source to share





All Articles