S E L L C O R P

TypeScript 5.4 Beta Unveiled -

TypeScript 5.4 Beta Unveiled

January 31, 2024 Garnet Comments Off

The latest iteration of TypeScript, Microsoft’s robustly-typed JavaScript variant, has entered the beta phase, bringing forth notable enhancements. This update focuses on maintaining type refinements within closures after the last assignment and introduces a novel utility type called NoInfer.

Scheduled for release, TypeScript 5.4 introduces several features, including the improved preservation of narrowing within function closures initiated post the last assignment. Accompanying this is the introduction of the NoInfer type, designed to obstruct inferences to valid but undesired types.

As of January 29, TypeScript 5.4 is available for installation through NuGet or NPM. For NPM, utilize the following command:

bashCopy code

npm install -D typescript@beta

One of the key advancements in TypeScript 5.4 lies in its more intelligent narrowing mechanism. Microsoft addresses a persistent issue where narrowed types were not consistently preserved within function closures. With the latest version, TypeScript 5.4 tackles this challenge by scrutinizing parameters and let variables in non-hoisted functions, identifying the last assignment point. Upon discovery, TypeScript can now perform narrowing from outside the encompassing function.

Additionally, TypeScript 5.4 introduces the NoInfer<T> utility type. Enclosing a type in NoInfer<…> signals TypeScript to match inner types for potential type inference candidates. This utility type resolves a problem where TypeScript could infer type arguments from input, leading to ambiguity about the best type to infer and resulting in TypeScript rejecting valid calls and making other errors.

Further enhancements in TypeScript 5.4 include:

  • Declarations for JavaScript’s new groupBy and Map.groupBy static methods, offering functionality to categorize elements based on distinct groups.
  • Support for require() calls in –moduleResolution bundler and –module preserve.
  • Verification of import attributes and assertions against the global ImportAttributes type, providing more accurate runtime descriptions of import attributes.
  • Quick Fix functionality allowing the addition of new parameters to functions called with excessive arguments.
  • More aggressive reduction of intersections with type variables and primitives by TypeScript, dependent on how the type variable’s constraint aligns with these primitives.
  • Deprecation of features from TypeScript 5.0, including target: ES3 code, NoImplcitUseStrict, charset, and out. TypeScript 5.4 is expected to be the final version where the list of deprecations operates conventionally.