Skip to content

type guard on (string & string literal) doesn't imply type guard on (string literal) #14210

Closed
@willportnoy

Description

@willportnoy

TypeScript Version: 2.1.6

Code

// A *self-contained* demonstration of the problem follows...
interface Base {
    readonly name: string;
}

interface DerivedA extends Base {
    readonly name: 'DerivedA',
    readonly A: string;
}

interface DerivedB extends Base {
    readonly name: 'DerivedB',
    readonly B: string;
}

type Derived = DerivedA | DerivedB;

type Both = Base & Derived;

// this function seems sufficient to hide the type from the compiler
function make(): Both {
    return {
        name: 'DerivedB',
        B: 'b',
    };
}

const derived = make();

// var name: (string & "DerivedB") | (string & "DerivedA")
if (derived.name === 'DerivedB') {
    // Property 'B" does not exist on type 'Both'
    console.log(derived.B);
}

Expected behavior:

I expect the type guard (derived.name === 'DerivedB') to narrow the type of derived from (Base & DerivedA) | (Base & DerivedB) to Base & DerivedB or DerivedB.

Actual behavior:

In the if block, derived.B is not available.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueIn DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions