Skip to content

Generic with extends and return will fail compilation #17794

Description

TypeScript Version: 2.4.2

class A {

}

export class B {
    public method<T extends A>(val: T): Promise<T> {
        return new Promise(resolver => {
            resolver(new A());
        });
    }
}

It fails with this
Argument of type 'A' is not assignable to parameter of type 'T | PromiseLike'.
Type 'A' is not assignable to type 'PromiseLike'.
Property 'then' is missing in type 'A'.

This code compiled just fine in 2.3

It looks similar to this old bug which compiled fine with 2.3, but fails with 2.4
#13993

Activity

  1. kitsonk commented on Aug 15, 2017

    @kitsonk
    Contributor

    Not reproducible in the playground.

  2. ahejlsberg commented on Aug 15, 2017

    @ahejlsberg
    Member

    This is working as intended and is an effect of #15104. Type A is not assignable to type T because T represents some type that derives from A.

    We also now handle #13993 correctly (by reporting an error), but for a different reason. It was fixed by #16368.

  3. jsgoupil commented on Aug 15, 2017

    @jsgoupil
    Author

    Anders Hejlsberg (@ahejlsberg) then maybe I am looking at this where T : A C# thing but in TypeScript?

    The documentation does not make any other mention of this? https://www.typescriptlang.org/docs/handbook/generics.html

    Is it possible in 2.4?

  4. mhegazy commented on Aug 29, 2017

    @mhegazy
    Contributor

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

  5. jsgoupil commented on Aug 29, 2017

    @jsgoupil
    Author

    Mohamed Hegazy (@mhegazy) could you kindly let me know if there is such a thing as I asked in the previous comment?
    I am staying on 2.3 until I figure this out...

  6. mhegazy commented on Aug 29, 2017

    @mhegazy
    Contributor

    Is it possible in 2.4?

    no. this is flagged as error in 2.4 +

  7. locked and limited conversation to collaborators on Jun 14, 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

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions