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:
- The function does not have an explicit return type or type predicate annotation.
- The function has a single return statement and no implicit returns.
- The function does not mutate its parameter.
- 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!