Microsoft/TypeScript
GitHub ã§èŠã*explicit* type guard passed to Array.filter does not narrow type
Open
#54,966 opened on 2023幎7æ11æ¥
Domain: check: Control FlowHelp WantedPossible Improvement
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
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[]