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 :
- Using proxy as object.property watcher in TypeScript
- typescript – how to resize base64 image in angular
- How to use yups object.shape with typescript?
- intellisense – Brackets.io in built support for TypeScript
- typescript – Visual Studio Code Automatic Imports
- typescript – How can I use TSLint in VS Code?
- typescript interface require one of two properties to exist
- javascript – Unit testing using Jasmine and TypeScript