Ein Blog

TypeScript 5.5 Beta.

TypeScript now infers a type predicate for the filter function.

// Previously, nums: (number | null)[]
// Now, nums: number[]
const nums = [1, 2, 3, null, 5].filter(x => x !== null);

Das war eine lange Baustelle und nun geht es endlich. Das Problem war lange Zeit, dass diese Lambdas keinen Type-Guard darstellen und somit die Information verloren geht, dass es bspw. nicht undefined ist. Das erkennt TS jetzt automatisch:
TypeScript will infer that a function returns a type predicate if these conditions hold:

  1. The function does not have an explicit return type or type predicate annotation.
  2. The function has a single return statement and no implicit returns.
  3. The function does not mutate its parameter.
  4. The function returns a boolean expression that’s tied to a refinement on the parameter.

But TypeScript now does basic syntax checking on regular expressions!

Praktisch!