TypeScript · Senior · Question 27 of 75
What changed about type predicate inference in TS 5.5?
const nums = [1, null, 2].filter(x => x !== null);- ✓Correct answer: TS infers the predicate
x is number, sonumsisnumber[] - 2
filternow always returnsnever[]for mixed arrays - 3Type predicates were removed in favor of assertion functions
Explanation
Previously nums was (number | null)[], and you had to write (x): x is number => x !== null. Now the compiler infers a predicate for simple functions if returning true really narrows the type.