8000 Type guard of union with conditional type not working since 4.3 Β· Issue #44382 Β· microsoft/TypeScript Β· GitHub
[go: up one dir, main page]

Skip to content
Type guard of union with conditional type not working since 4.3Β #44382
Closed
@pedro-pedrosa

Description

@pedro-pedrosa

Bug Report

πŸ”Ž Search Terms

type guard conditional type

πŸ•— Version & Regression Information

  • This changed between versions 4.2.3 and 4.3.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

interface A<S> {
    a: S
}
interface B<S> {
    b: S
}
interface C<S> {
    c: S
}

type U1<S> = A<S> | B<S> | C<S>
function f1<S>(u: U1<S>): u is B<S> | C<S> {
    return false
}
function test1<S>(x: U1<S>) {
    if (!f1(x)) {
        x.a //OK
    }
}

type Cond<S> = S extends number ? B<S> : C<S>
type U2<S> = A<S> | Cond<S>
function f2<S>(u: U2<S>): u is Cond<S> {
    return false
}
function test2<S>(x: U2<S>) {
    if (!f2(x)) {
        x.a //ERROR
    }
}

πŸ™ Actual behavior

The type guard is unable to narrow the type of x to A

πŸ™‚ Expected behavior

The type guard should narrow the type of x to A because it is not assignable to Cond<S>

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0