question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spread operator fails for object type generics

See original GitHub issue

TypeScript Version: 3.0

Search Terms: generic, spread, object types, spread types, spread operator

Code

function shallowCopy<T extends object>(state: T): T {
  return {
    ...state
  }
}

Expected behavior: Should compile as only state’s properties are being shallow-copied to the returned result and thus meets the T type.

Actual behavior: Does not compile with the following error message:

> Spread types may only be created from object types.

If T extends object, is it not then an object type? If not, what type or interface can be defined that is an object type and is not exactly {} or object?

Possible ways forward:

  • If the compiler does not properly recognize T extends object as an object type, perhaps this should be implemented to better support spread/rest operators in modern ecmascript syntax.
  • If T extends object is a recognized object type, the compiler error should be amended to something like “Spread types may only be created from the object type in generics”
  • Silently lock and close this issue adding documentation that states “The spread operator does not support generics extending object

Playground Link: Contrived example

Related Issues: https://github.com/Microsoft/TypeScript/pull/13288 - While this seems like the use case was addressed by @RyanCavanaugh , the resulting workaround suggests far more explicit typing than should be required by the compiler to infer the types in the example provided. While closing that PR might have been the protocol, it seemed more effectively to silence the community request for spread operator support with generics extending from object.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:25
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
RyanCavanaughcommented, Oct 9, 2018

Silently lock and close this issue

:extremely long sigh:

6reactions
alamothecommented, Aug 17, 2018

Came here because of:

type A = {
	a: string
	b: number
}

function f<B extends object>(x: A & B) {
	const {a, b, ...other} = x // does not work
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Object spread doesn't work with generics in TypeScript? Why ...
i find that in example one, the {...car} gives the following typescript error: error TS2698: Spread types may only be created from object...
Read more >
Spread types may only be created from object types in TS
The "Spread types may only be created from object types" error occurs when we try to use the spread operator with a value...
Read more >
Issues with understanding generics and typing array spread ...
Issues with understanding generics and typing array spread operator. Hello,. I'm having issues with translating my idea into code - namely, ...
Read more >
The Typescript Object Spread Operator - YouTube
This video is part of the Typescript: The Ultimate Bootcamp Course - https://angular-university.io/course/typescript-bootcampCheck out the ...
Read more >
Spreading generic tuple parameters - Learn TypeScript
We are going to start this lesson by exploring a function that uses the spread operator and discover a problem with the typing....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found