microsoft / microsoft/TypeScript

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

Open
#20,583 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided example with the TypeScript compiler, preferably against typescript@next as requested. Trace the truthiness-guard narrowing behavior and compare it with the stated in type-guard behavior; done means x narrows to never in the false branch while preserving the expected type of x.a.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.