Dev

TypeScript 5.3 adds support for import attributes


TypeScript 5.3, an upgrade to Microsoft’s strongly typed JavaScript variant that adds support for import attributes for ECMAScript modules, has moved to a release candidate (RC) stage. The RC, announced November 3, adds an option to prefer type-only auto-imports when possible.

Previously, when TypeScript generated auto-imports for something in a type position,  it would add a type modifier based on the developer’s settings. With a recent change, TypeScript now enables an editor-specific option. For example, when getting an auto-import on Person in the following:

export let p: Person

TypeScript’s editing experience would usually add an import for Person as:

import { Person } from "./types"; export let p: Person

and under certain settings, like verbatimModuleSyntax, TypeScript would add the type modifier:

import { type Person } from "./types"; export let p: Person

However, if a code base is not able to use some of these options, or if a developer simply has a preference for explicit type imports when possible, TypeScript now enables this via an editor-specific option.

The RC follows a beta release published October 3. No further changes are expected between now and the planned stable release on November 14, apart from critical bug fixes. TypeScript 5.3 also is set to support updates to an import attributes proposal for ECMAScript modules. 

The ECMA standards proposal features an inline syntax for module import statements to pass information alongside the module specifier. These attributes will support additional types of modules in a common way across JavaScript environments, beginning with JSON modules.

In an October 3 bulletin on the TypeScript 5.3 beta, Microsoft said one use case of import attributes was to provide information about the expected format of a module at runtime. In the example cited, Microsoft said the contents of attributes were not checked by TypeScript because they are host-specific and left alone so that browsers and runtimes can handle them.

Import attributes are an evolution an earlier proposal, import assertions, that were implemented in TypeScript 4.5 in November 2021. The most obvious difference is the use of the with keyword over the assert keyword. A less visible difference is that runtimes now can use attributes to guide the resolution and interpretation of import paths, while import assertions only could assert some characteristics after loading a module. Plans call for deprecating the old syntax for import assertion in favor of the proposed standard for import attributes.

The TypeScript 5.3 RC can be accessed via NuGet, or via NPM using the following command:

npm install -D typescript@rc

TypeScript 5.3 is due in a final production release on November 14. Predecessor TypeScript 5.2 was released August 24.

Other new features and improvements coming in TypeScript 5.3:

  • TypeScript 5.3 supports the resolution-mode attribute for import type.
  • Narrowing can be performed based on conditions in each case clause with a switch (true).
  • TypeScript 5.3 more closely inspects super property accesses and method calls to see if they correspond to class fields. If they do, a type-checking error will result.
  • TypeScript inlay hints now support jumping to the definition of types, making it easier to casually navigate code.
  • When running TypeScript via tsc, the compiler will avoid parsing JSDoc, reducing parsing time and memory usage to store comments along with time spent in garbage collection.
  • Consolidation has been done between tsserverlibrary.js and typescript.js.
  • TypeScript now detects when the declaration referenced by a super. property access is a class field and issues an error, thus preventing errors that could occur at runtime.
  • In JavaScript it’s possible to override the behavior of the instanceof operator. To do this, the value on the right of the operator must have a specific method named by Symbol.hasInstance. To better model this behavior in instanceof, TypeScript now checks if such a [Symbol.Instance] method exists and is declared as a type predicate function. If so, the tested value on the left side of the instanceof operator will be narrowed appropriately by that type predicate.

Copyright © 2023 IDG Communications, Inc.



READ SOURCE

This website uses cookies. By continuing to use this site, you accept our use of cookies.