-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spread operator fails for object type generics #26412
Comments
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? |
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
} |
Just linking this for reference: |
:extremely long sigh: |
Reading #20013, I understand that For example: type MyObj = {
a: string,
b: number,
c: boolean,
}
function foo<T extends MyObj>(value: T) {
const { a, ...otherValues } = value;
} is it possible to infer Regarding #20013, is it possible to have a helper type |
This bug is probably related to #8856 somehow. |
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? |
@JoshuaKGoldberg Isn't that the problem? It should give a compile-time error ideally.
|
TypeScript Version: 3.0
Search Terms:
generic, spread, object types, spread types, spread operator
Code
Expected behavior:
Should compile as only
state
's properties are being shallow-copied to the returned result and thus meets theT
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 anobject type
? If not, whattype
orinterface
can be defined that is anobject type
and is not exactly{}
orobject
?Possible ways forward:
T extends object
as an object type, perhaps this should be implemented to better support spread/rest operators in modern ecmascript syntax.T extends object
is a recognized object type, the compiler error should be amended to something like "Spread types may only be created from theobject
type in generics"object
"Playground Link: Contrived example
Related Issues:
#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
.The text was updated successfully, but these errors were encountered: