Skip to content

Infer more specific type from a union of tuple types when using string literals #14339

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

Closed
fongandrew opened this issue Feb 27, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@fongandrew
Copy link

TypeScript Version: 2.2.1

Code

declare var x: { type: "A", value: number; } | { type: "B", value: string; };
if (x.type === "A") {
    x.value; // Correctly inferred to be number
}

declare var y: ["A", number] | ["B", string];
if (y[0] === "A") {
    y[1]; // Not inferred as number but as string | number
}

Expected behavior: y[1] should be inferred as a number based on the value of y[0]

Actual behavior: y[1] is inferred to be string|number.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 28, 2017

Duplicate of #10530

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 28, 2017
@mhegazy mhegazy closed this as completed Feb 28, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

2 participants