top of page

What's new in TypeScript 4.7?



TypeScript 4.7, now in a beta release, offers ECMAScript module (ESM) support for Node.js 12 as well as a multitude of coding enhancements.


Unveiled April 8, TypeScript 4.7 is the latest planned version of Microsoft’s strongly typed JavaScript. TypeScript 4.7 backs ECMAScript module support in Node.js 12, something that had been planned for TypeScript 4.5 late last year but was delayed.


TypeScript 4.7 is due to reach a release candidate stage on May 10, followed by a final release on May 24. The beta can be accessed via NPM:

npm install typescript@beta

Capabilities in TypeScript 4.7 include:

  • To control module detection, TypeScript 4.7 introduces the option moduleDetection.

  • A moduleSuffixes option is supported to customize lookup of module specifiers.

  • With control flow analysis for computer properties, TypeScript analyzes the type of computed properties and narrows them correctly.

  • TypeScript now can perform more granular inferences from functions with objects and arrays. This allows types of these functions to consistently flow in a left-to-right manner, just like for plain arguments.

  • Functions and constructors can be fed type arguments directly.

  • Developers can explicitly specify variance on type parameters.

  • typeof queries can be performed on private fields.

  • Organize Imports is performed in a group-aware manner.

  • Snippet completions are provided for object literal methods.

  • Under strictNullChecks, the type checker disables a type safety hole that was maintained for backward compatibility, where type parameters were considered to be assignable to {} and object. This is a breaking change.

  • A readonly tuple now will treat its length property as readonly. This also is a breaking change.


Features of TypeScript 4.7


1. ECMAScript Module Support in Node.js

ECMAScript is a JavaScript standard meant to ensure the interoperability of web pages across different web browsers. Node.js is built on a very different module called CommonJS, this has made ECMAScript support a very difficult feature to implement. A few releases back experimental support was added in nightly builds to gather feedback from users. Now TypeScript 4.7 adds two modules to support this functionality, node12 and nodenext.

{
    "compilerOptions": {
        "module": "nodenext",
    }
}



2. CommonJS Interop

Now you can import CommonJS modules as if they were ES modules with a default export. Node.js may also synthesize named exports from CommonJS modules in some circumstances.

// ./foo.cts
export function helper() 
{
    console.log("hello world!");
}

// ./bar.mts
import { helper } from "./foo.cjs";

// prints "hello world!"
helper();


3. Improved Function Inference in Objects and Methods

TypeScript 4.7 can now infer with more granularity from functions contained within objects and arrays. This allows the types of these functions to flow consistently from left to right, exactly like ordinary arguments. You can see this in action below in this screenshot courtesy of Daniel Rosenwasser Senior Program Manager, TypeScript.




The Tech Platform

0 comments

Recent Posts

See All
bottom of page