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
Spread operator does not work with 2.9.0-dev20180503 for generic but there is workaround by using Object.assign. Both should work. Currently, the spread syntax returns "Spread types may only be created from object types."
You want to merge two objects, one (or more) is generic.
Examples
interface WithId {
id: number;
}
interface User {
name: string;
}
interface Developer extends User {
favoriteLanguage: string;
}
function identifyUser<T extends User>(user: T): T & WithId {
const newUser = Object.assign({}, user, { id: 1 });
return newUser;
}
function identifyUser2<T extends User>(user: T): T & WithId {
const newUser = {
...user, // This is not compiling
id: 1
};
return newUser;
}
Checklist
My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered:
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Search Terms
spread, generic, intersection
Suggestion
Spread operator does not work with 2.9.0-dev20180503 for generic but there is workaround by using Object.assign. Both should work. Currently, the spread syntax returns "Spread types may only be created from object types."
Related to this issue #13557
Use Cases
You want to merge two objects, one (or more) is generic.
Examples
Checklist
My suggestion meets these guidelines:
[x] This wouldn't be a breaking change in existing TypeScript / JavaScript code
[x] This wouldn't change the runtime behavior of existing JavaScript code
[x] This could be implemented without emitting different JS based on the types of the expressions
[x] This isn't a runtime feature (e.g. new expression-level syntax)
The text was updated successfully, but these errors were encountered: