
TypeScript 5.4, a planned update to the strongly typed JavaScript variant from Microsoft, has reached beta availability. New capabilities include preserved narrowing within function closures created after the last assignment and a NoInfer type to block inferences to valid but unwanted types.
Released January 29, TypeScript 5.4 can be accessed via NuGet or NPM. In NPM, use the following command:
npm install -D typescript@beta
TypeScript 5.4 makes narrowing smarter. Detailing the improvement, Microsoft said a common pain point in TypeScript was that narrowed types were not always preserved within function closures. In TypeScript 5.4, when parameters and let
variables are used in non-hoisted functions, the type checker will look for a last assignment point. If one is found, TypeScript can narrow from outside the containing function.
TypeScript 5.4 also introduces a NoInfer<T>
utility type. Surrounding a type in NoInfer<…>
gives a signal to TypeScript to match the inner types to find candidates for type inference. The utility type addresses an issue in which TypeScript can infer type arguments from whatever is passed in. But it is not always clear what is the best type to infer, leading TypeScript to reject valid calls and make other mistakes.
Other improvements in TypeScript 5.4:
- Declarations are added for JavaScript’s new
groupBy
andMap.groupBy
static methods.Object.groupBy
takes an iterable and a function deciding which group each element should be placed in. The function must make a key for each distinct group, andObject.groupBy
uses that key to make an object where every key maps to an array containing the original element.Map.groupBy
is similar, but produces a Map rather than a plain object. - Support has been added for
require( )
calls in--moduleResolution bundler
and--module preserve
. - Import attributes and assertions are now checked against the global
ImportAttributes
type. This means runtimes now can more accurately describe the import attributes. - A Quick Fix allows you to add a new parameter to functions called with too many arguments.
- TypeScript now reduces intersections with type variables and primitives more aggressively, depending on how the type variable’s constraint overlaps with these primitives.
- Deprecations have been added from TypeScript 5.0 such as
target: ES3
code,NoImplcitUseStrict
,charset
, andout
. TypeScript 5.4 likely will be the last version in which the list of deprecations continues to function as normal.
Next read this: