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

Generic type not narrowed using flow control analysis #16904

Closed
magnushiie opened this issue Jul 3, 2017 · 1 comment
Closed

Generic type not narrowed using flow control analysis #16904

magnushiie opened this issue Jul 3, 2017 · 1 comment

Comments

@magnushiie
Copy link
Contributor

TypeScript Version: 2.4.0

Code

// A *self-contained* demonstration of the problem follows...

function testUnion() {
    type X = "A" | "B";

    function test(x: X) {
        if (x === "A") {
            const copyA: "A" = x; // succeeds, x's type is "A"
            // const copyB: "B" = x; // error
        }
    }
}

function testExtends() {
    type X = "A" | "B";

    function testZ<Z extends X>(x: Z) {
        if (x === "A") {
            const copyA: "A" = x; // error, x's type is still Z extends "A" | "B"
            // const copyB: "B" = x; // error, as expected
        }
    }
}

Expected behavior:
The initialization of copyA in testExtends should succeed.

Actual behavior:

src/test.ts(17,19): error TS2322: Type 'Z' is not assignable to type '"A"'.
  Type 'X' is not assignable to type '"A"'.
    Type '"B"' is not assignable to type '"A"'.

This prevents some useful generic implementations where the actual type of X is a keyof type expression, and other types are resolved based on the type of x.

@magnushiie
Copy link
Contributor Author

Ah, this seems to be a duplicate of #13995

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant