Skip to content

Narrow the parent type of a singleton type to never in the false branch #20583

Open
@weswigham

Description

@weswigham

If the type of the member checked is not falsy (and so is never in the false branch).

TypeScript Version: 2.7.0-dev.201xxxxx

Code

const x: {a: 1, b: 2} = { a: 1, b: 2 };
if (x.a) {

}
else {
    x.a;
}

Expected behavior:
type of x in the else branch is never.

Actual behavior:
type of x is {a: 1. b: 2}, and x.a is (counterintuitively) never.

This would bring the behavior of truthiness guards in line with our new behavior for in type guards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In 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