Skip to content

Generic type is not expanded when typechecking equality check #26758

@fictitious

Description

@fictitious

TypeScript Version: 3.1.0-dev.20180829

Search Terms: generics expansion union equality typecheck

Code

type A = {type :"a"}
type B = {type :"b"}

type P<T extends A | B> = T["type"];

class C<T extends A | B> {

    p: P<T> = 'x'; //error: Type '"x"' is not assignable to type '"a" | "b"'
    // so when typechecking this assignment, it's known that 
    // P<T> actually is "a" | "b"

    f(p: P<T>) {
        if (p === 'x') { // why no error here
        // when typechecking this equality check, P<T> is not expanded ?   
        }
    }

    // expected error, for illustration
    f0(p: 'a' | 'b') {
        if (p === 'x') { // error: This condition will always return 'false' 
                         // since the types '"a" | "b"' and '"x"' have no overlap.

        }
    }

}

Expected behavior:

"This condition will always return 'false'" is reported when doing equality check between a variable declared with P<T> type and "x" literal

Actual behavior:

No error is reported for that particular equality check

Playground Link: link

Related Issues: did not find anything

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions