microsoft / microsoft/TypeScript

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

Aperta
#61,770 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

🔎 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:

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

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

Image

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

Image

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Usa gli esempi collegati di TypeScript Playground come punto di ingresso; confronta il restringimento del flusso di controllo per args.length > 0 con i casi esistenti di truthiness e uguaglianza. Il lavoro è completato quando example1 restringe [number] | [] a [number] nel ramo true senza perdere il comportamento mostrato da examples 2 e 3.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.