You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Spread operator fails for object type generics #26412
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"
Related Issues: #13288 - While this seems like the use case was addressed by Ryan Cavanaugh (@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.
Just digging through those links, #10727 (comment) suggests that there wasn't a specific reason for this to not work originally, but by #20013 it was decided that this was correct to reject this.
On the one hand, the reasoning there, essentially that own/enumerable requirement for spread isn't modeled by TS, is also an issue even without generics so allowing this usage isn't introducing a new type-safety problem; on the other, the problem is much larger with generics, as the heuristic TS uses for non-generic spreads (which is just strip methods, right?) doesn't work. Is this why #10727 was recently tagged as Revisit?
Reacted by Simon Buchan, Ole Ersoy, Jason Killian, Ilya Doroshin, Gerald Davis, Song Seunggeun, Rauno Viskus, Nick Partridge, Josh Ghoulberg 👻, Zzzen and 2 more
Reading #20013, I understand that ...T is not assignable T. But may I know the challenge for getting a correctish type inference?
For example:
typeMyObj={a: string,b: number,c: boolean,}functionfoo<TextendsMyObj>(value: T){const{ a, ...otherValues}=value;}
is it possible to infer otherValues as Omit<MyObj, 'a'> & AnyObject, so we can at least make it type-safe if we access properties in otherValues that belongs to MyObj?
Regarding #20013, is it possible to have a helper type RemovePrototype<T> or something?
Activity
simonbuchan commentedon Aug 16, 2018
Just digging through those links, #10727 (comment) suggests that there wasn't a specific reason for this to not work originally, but by #20013 it was decided that this was correct to reject this.
On the one hand, the reasoning there, essentially that own/enumerable requirement for spread isn't modeled by TS, is also an issue even without generics so allowing this usage isn't introducing a new type-safety problem; on the other, the problem is much larger with generics, as the heuristic TS uses for non-generic spreads (which is just strip methods, right?) doesn't work. Is this why #10727 was recently tagged as Revisit?
alamothe commentedon Aug 17, 2018
Came here because of:
oleersoy commentedon Sep 23, 2018
Just linking this for reference:
https://stackoverflow.com/questions/52469942/deep-cloning-entity-instances-in-typescript?noredirect=1#comment91881873_52469942
RyanCavanaugh commentedon Oct 9, 2018
:extremely long sigh:
Cryrivers commentedon Oct 16, 2018
Reading #20013, I understand that
...Tis not assignableT. But may I know the challenge for getting a correctish type inference?For example:
is it possible to infer
otherValuesasOmit<MyObj, 'a'> & AnyObject, so we can at least make it type-safe if we access properties inotherValuesthat belongs toMyObj?Regarding #20013, is it possible to have a helper type
RemovePrototype<T>or something?xiaodeaux commentedon Dec 7, 2018
This bug is probably related to #8856 somehow.
JoshuaKGoldberg commentedon Mar 9, 2021
This no longer gives an error in the latest version: playground link with collected examples from linked issues. Time to close the issue as resolved?
mnpenner commentedon Mar 9, 2021
Josh Ghoulberg 👻 (@JoshuaKGoldberg) Isn't that the problem? It should give a compile-time error ideally.
shallowClone(new C())doesn't have the methodm().