Skip to content

Unexpected union type between generic and non-generic function #12970

Description

@gcnew

TypeScript Version: nightly (2.2.0)

Code

function id<T>(x: T): T {
    return x;
}

function stringId(x: string): string {
    return x;
}

function numberId(x: number): number {
    return x;
}

// OK: ((x: string) => string) | ((x: number) => number)
const f = true ? numberId : stringId;

// Actual: <T>(x: T) => T
// Expected: (x: string) => string
const g = true ? id : stringId;

Expected behavior:
Union between a function and a generic function should yield the most specific type, not the most general one.

Activity

  1. JsonFreeman commented on Mar 22, 2017

    @JsonFreeman
    Contributor

    I agree with the expected behavior. I also wonder if it would be reasonable to not collapse the union and keep both constituents.

  2. gcnew commented on Jun 9, 2017

    @gcnew
    ContributorAuthor

    Fixed by #16368

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

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions