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);

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.

Further reading

Problem with a question

TypeScript · Senior · question 27

Test yourself

Take the TypeScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz