Skip to content

Associativity fails for intersections of unions #19055

Closed
@tenedor

Description

@tenedor

TypeScript Version: 2.5.1-insiders.20170825

Code

interface A {
    identity: 'a';
    a: number;
}
interface B {
    identity: 'b';
    b: number;
}
interface C {}

// This function errors as written. It does not error if changed to `value: C & (A | B)`.
function f(value: ((A | B) & C) & (A | B)): number {
    switch (value.identity) {
        case 'a':
            return value.a; // Error: Property 'a' does not exist on 'B & C'.
        case 'b':
            return value.b; // Error: Property 'b' does not exist on 'A & C'.
    }
}

Expected behavior:
No error. This is a discriminated union and the switch statement should infer property a.

Furthermore, I expect that the following types are equivalent:

  1. ((A | B) & C) & (A | B)
  2. (A | B) & C & (A | B)
  3. C & (A | B)

However, 1 behaves differently from 2 and 3.

Actual behavior:

TS errors because it fails to infer that the type cannot be B & C in the first case.

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