Skip to content
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 and Generic #24060

Closed
MrDesjardins opened this issue May 11, 2018 · 2 comments
Closed

Spread and Generic #24060

MrDesjardins opened this issue May 11, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@MrDesjardins
Copy link

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

    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)

@mhegazy
Copy link
Contributor

mhegazy commented May 11, 2018

Duplicate of #10727. Fixed by #13288

@mhegazy mhegazy added the Duplicate An existing issue was already created label May 11, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants