Skip to content

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

Closed
@fongandrew

Description

@fongandrew

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.

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