Typescript: Spread types may only be created from object types

Typescript: Spread types may only be created from object types

Typescript: Spread types may only be created from object types

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

n


n

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

n

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

n

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

n

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

n

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

You can use blank curly brackets {} or an interface like below examples:

n

goodsArray.map(good => {n  return {n    id: good.payload.doc.id,n    ...good.payload.doc.data() as {}n  };n});n

n

or

n

goodsArray.map(good => {n  return {n    id: good.payload.doc.id,n    ...good.payload.doc.data() as Goods // Goods is my interface namen  };n});n

Typescript: Spread types may only be created from object types

Version 3.2 of Typescript fixed this. The two PRs that improve handling of spread and rest parameters are:

n

n

n

n

You can try it out now using npm install [email protected].

n

With 3.2 your code works as is.

n

Version 3.2 has been released on November 29th, 2018, you can read more about it here.

Related posts on Type script :

Leave a Reply

Your email address will not be published.