Skip to content

Spread operator should work with generic types #13557

Closed

Description

TypeScript Version: master

Code

function f<a extends Object>() {
	return {...<a>{}};
}
function g<a extends object>() {
	return {...<a>{}};
}

Expected behavior:

ok

Actual behavior:

index.ts(2,10): error TS2698: Spread types may only be created from object types.
index.ts(5,10): error TS2698: Spread types may only be created from object types.

Activity

  1. HerringtonDarkholme commented on Jan 18, 2017

    @HerringtonDarkholme
    Contributor

    Another example, it's quite hard for TypeScript to prove a type constraint is spreadable given structural typing

    class NonPrimitive {
        private cannotBePrimitiveBrand
    }
    function t<T extends NonPrimitive>(a: T, b: NonPrimitive) {
        var c = { ...a }
        var d = { ...b }
    }

    #2103 (comment)

  2. billba commented on Apr 24, 2017

    @billba
    Member

    Another example:

    interface Foo {
        foo: string;
    }
    
    const addBar = <F extends Foo>(input: F) => ({
        ... input,
        bar: 5
    });

    Build 2.3.0-dev.20170424 gives error TS2698: Spread types may only be created from object types. on ...input,

  3. mhegazy commented on Apr 24, 2017

    @mhegazy
    Contributor

    Duplicate of #10727. Fixed by #13288

  4. billba commented on Apr 24, 2017

    @billba
    Member

    Mohamed Hegazy (@mhegazy) This is still happening in the current build.

  5. mhegazy commented on Apr 24, 2017

    @mhegazy
    Contributor

    yes. #13288 is not merged yet. we do not have an ETA at the moment.

  6. billba commented on Apr 24, 2017

    @billba
    Member

    Aha, thanks.

  7. LKay commented on Apr 25, 2017

    @LKay

    When it will be fixed finally? The problem persists and it's breaking code all over the place!

  8. devlato commented on Apr 20, 2018

    @devlato

    Any plans fixing this?

  9. massimonewsuk commented on May 3, 2018

    @massimonewsuk

    Is there a milestone or version that we can expect this to be fixed in?

  10. nblavoie commented on May 12, 2018

    @nblavoie

    Count me in !

  11. mcamou commented on Jul 23, 2018

    @mcamou

    Any news on when/whether #13288 will be merged? This bug has been open for over a year now.

  12. ivansky commented on Aug 23, 2018

    @ivansky

    Mohamed Hegazy (@mhegazy)
    It's not a duplicate! I am interested in fixing too.
    Could you remove label and reopen the issue?

  13. RyanCavanaugh commented on Aug 24, 2018

    @RyanCavanaugh
    Member

    Please read #13288 (comment)

  14. locked as resolved and limited conversation to collaborators on Aug 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions