microsoft / microsoft/TypeScript
Allow narrowing of unions discriminated by numeric literals using `>` `<` etc
Nessuno ha ancora preso questa issue.
- 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
💻 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":
Type inference on "args" after the check for "example2":
Type inference on "args" after the check for "example3":
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
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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