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)
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
Activity
mhegazy commentedon May 11, 2018
Duplicate of #10727. Fixed by #13288
typescript-bot commentedon May 30, 2018
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.