Typescript: Spread types may only be created from object types

This is fixed in TypeScript Version 3.2. See Release Notes.


Looks like spread with a generic type isn’t supported yet, but there is a GitHub issue about it: Microsoft/TypeScript#10727.

For now you can either use type assertion like @Jevgeni commented:

function foo<T extends object>(t: T): T {
  return { ...(t as object) } as T;
}

or you can use Object.assign which has proper type definitions.

function foo<T extends object>(t: T): T {
  return Object.assign({}, t);
}

Leave a Comment