Skip to main content

My favourite part of TS 5.5 beta

One of the most painful things in TypeScript was the inability to infer types in predicates. For example, you have a collection with possibly null or undefined values and you want to filter them out. Before, even with filtering such values out, TypeScript was unable to infer the type of item of the collection:

const collection = [1.1, 2.3, null, 3.5, undefined, 4.5];
const filtered = collection.filter(v => {
return v !== null && v !== undefined;
});

/**
* @note Error: 'value' is possibly 'null' or 'undefined'
*/

filtered.forEach(value => value.toFixed());

Link to playground

But with TypeScript 5.5 beta, you can finally do this and TypeScript will infer the type of the collection correctly, so it will know that filtered is an array of numbers without null or undefined values. But the trick, that I use sometimes to simplify the code, unfortunately, doesn't work:

const collection = [1.1, 2.3, null, 3.5, undefined, 4.5];
const filtered = collection.filter(Boolean);

filtered.forEach(value => value.toFixed());

Link to playground

But anyway, I'm happy that it's minus one thing that make logic of the code and type system of TypeScript inconsistent.

The rest of the features of TypeScript 5.5 beta can be found in the official announcement.