A past puzzle — fully playable. 4 attempts, hints on wrong guesses.
stdle-16.ts
1functionisNum(x:unknown):xisnumber{
2returntypeofx==="number"
3}
4constr=[1,"a",2].filter(isNum)
5console.log(r.length)
Narrowing
Answer & explanation
Console output
2
Why
When a type predicate is passed to `filter`, TypeScript narrows the result array to `number[]`, but the runtime behaviour is ordinary `filter`: it keeps elements where the predicate returns true. Two elements (1 and 2) are numbers, so the resulting length is 2. The predicate`s narrowing and its boolean filtering are the same call.