microsoft / microsoft/TypeScript
*explicit* type guard passed to Array.filter does not narrow type
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Bug Report
🔎 Search Terms
array.filter, type guard, explicit, is
🕗 Version & Regression Information
TS 3.9.7–5.1.6, target version ES2019 or later (releases earlier than 3.9.7 didn’t have Object.fromEntries)
⏯ Playground Link
Playground link with relevant code
💻 Code
interface Element { type: Elem['type'], children: Node_[] }
interface CodeBlock extends Element { type: 'cb', children: string[] }
interface Field extends Element { type: 'f', name: string }
interface FieldList extends Element { type: 'fl', children: Field[] }
type Elem = CodeBlock | Field | FieldList
type Node_ = Elem | string
function getMeta2(fieldLists: Elem) {
return Object.fromEntries(
fieldLists.children
.filter((n: Node_): n is FieldList => typeof n !== 'string' && n.type === 'fl')
.flatMap((fl) => fl.children)
.map((f) => [f.name, f.children[0]?.toString()]),
)
}
🙁 Actual behavior
> .flatMap((fl) => fl.children)
Property 'children' does not exist on type 'Node_'.
Property 'children' does not exist on type 'string'.
🙂 Expected behavior
The return type of filter should be FieldList[]
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
Inizia con il Playground collegato e l’esempio ridotto di Array.filter nell’issue, quindi segui come viene verificato l’esplicito callback di type guard e come viene inferito il tipo del risultato di filter. Conferma la correzione verificando che il valore filtrato venga trattato come FieldList[] e che l’accesso successivo a flatMap sia accettato.
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
- Abbastanza chiara
- Idoneità per principianti
- 35/100