TypeScript 5.4 adds the NoInfer utility type

Microsoft just announced it TypeScript 5.4 is now available. TypeScript is an extension to JavaScript that adds static types to the language, created and maintained by Microsoft.

TypeScript 5.4 introduces features such as the NoInfer helper type, the ability to preserve type specification for closures created after the last assignment, and declarations for JavaScript’s Object.groupBy and Map.groupBy.

The NoInfer utility type tells TypeScript not to try to match internal types to find a candidate for type inference. This was necessary because TypeScript usually tries to infer type arguments based on what you pass, but there are situations where you might not want it to make those assumptions.

According to Microsoft, TypeScript could also make assumptions based on a check you performed to determine a more specific type for a variable, but those new types weren’t always preserved in function closures. In this latest release, it now preserves “type refinements for parameters, local let variables, and catch clause variables in closures that were created after the last assignment to those parameters or variables”, PR says.

This edition also adds new declarations go with JavaScript’s new static Object.groupBy and Map.groupBy methods. Object.groupBy is used in iterables and uses a function that determines the group in which each element of the iterable should be placed and then creates a key for each group. It then uses the key to create an object where the key is mapped to an array containing the original element. Map.groupBy is similar and produces a map instead of an object.

Other new features in this release include:

  • Ability to use require() when setting preservation module settings, which is a new available option
  • Validation of import attributes and assertions against the ImportAttribute type
  • A quick fix for adding a new parameter to functions that are called with too many arguments
  • Support for “subpath imports”

Microsoft also deprecated a number of options in TypeScript 5.4, including chatset, importsNotUsedAsValues, noImplicitUseStrict, and more.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *