microsoft / microsoft/TypeScript

Allow narrowing of unions discriminated by numeric literals using `>` `<` etc

Open
#61,770 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

### 🔎 Search Terms

"arguments length"

### 🕗 Version & Regression Information

It works the same in v3.3.3

### ⏯ Playground Link

https://www.typescriptlang.org/play/?target=99&module=0&ts=5.8.3#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsgRgAoA6M9AJwHMBnALkQG0wRMAjZCgXUQB8muASkQBvAFCJJiGMERFKtEvjBUoAC0QA+RAAZh4qYcQVkUEBSQKajHVwDcEqQF8UuGslGOjx0+aQA5dH8HQycxMNBIWAQUDBx8ACZScmp6JhZ2Th5+RiFPQxk5KyVkFXV9LyMTMwtEKxt7SpdkNw8Dbx8agKCQ53CxSOh4JDQsPGQAZmSSKwZmVg5uPgEKgtl5VJKyjQBePd1Vjuq-REDgptd3fKPfWvrbXskwpyA

### 💻 Code

Some basic ways to check if we have any arguments could be these:

```ts
function example1(...args: [number] | []) {
if (args.length > 0) {
return args[0];
} else {
return NaN;
}
}
function example2(...args: [number] | []) {
if (args.length) {
return args[0];
} else {
return NaN;
}
}
function example3(...args: [number] | []) {
if (args.length === 0) {
return NaN;
} else {
return args[0];
}
}
```

But TS doesn't seem to understand the first one.

### 🙁 Actual behavior

It doesn't understand the way it's done in "example1".

### 🙂 Expected behavior

It should understand the way it's done in "example1".

### Additional information about the issue

Type inference on "args" after the check for "example1":

![Image](https://github.com/user-attachments/assets/02538891-167b-459d-8fe8-ec966ad97990)

Type inference on "args" after the check for "example2":

![Image](https://github.com/user-attachments/assets/502573b5-346c-4eb9-9db6-89452c9f35b1)

Type inference on "args" after the check for "example3":

![Image](https://github.com/user-attachments/assets/aee70d26-b2b6-40c9-a9a0-555b0f914a55)

Notice how for "example2" and "example3" the type is correctly narrowed to just `[number]`, but for "example1" the unnarowed `[number] | []` type remains.

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

Use the linked TypeScript Playground examples as the entry point; compare control-flow narrowing for args.length > 0 with the existing truthiness and equality cases. Done means example1 narrows [number] | [] to [number] in the true branch without losing the behavior shown by examples 2 and 3.

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
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.