The TypeScript team announced the release of TypeScript 4.8 beta and TypeScript 4.7, which introduce ES Module (ESM) support for Node.js, improved type inference and control flow analysis, and significant performance improvements.
Since ES6 introduced modules in 2015, work has been done to move the JavaScript and TypeScript ecosystems to the native module format. Early use was mostly limited to authoring, with build tools like Webpack and transpilers like TypeScript converting code into modules that would run in different environments.
As the module format has improved over the past few years, browsers natively support ESM loading, and Node.js 16 already does. The TypeScript 4.7 release helps us move closer to a world where all JavaScript is built and used as an ESM.
Daniel Rosenwasser, TypeScript Program Manager, explains:
Over the past few years, Node.js has been working to support ECMAScript Modules (ESM). This was a very difficult feature because the Node.js ecosystem is built on a different module system called CommonJS (CJS). The interaction between the two leads to great challenges, with many new features to juggle.
TypeScript 4.7 adds two new module settings: node16 and nodenext. By using “type”: “module” on package.json, Node.js determines whether .js files are interpreted as ESM or CommonJS modules. ESM supports key features including top-level import/export and async/await reporting.
Relative import paths with ESM need full file extensions in the path, and various techniques used by CommonJS modules are not supported, such as requirement and top-level module.
Node.js supports two new file extensions for modules that are always ESM or CJS, .mjs and .cjs, so TypeScript added counterparts, .mts and .cts.
These releases add more than Node.js ESM support. Control flow analysis of element accesses in parentheses helps to narrow the types of element accesses when indexed keys are literal types and unique characters. The –strictPropertyInitialization flag now checks that computed properties are initialized before the end of the constructor body.
TypeScript 4.7 also supports more verbose type inference from functions on objects and arrays. New support for instantiation expressions allows narrowing of generics during instantiation.
TypeScript 4.8 adds many correctness and consistency improvements to the –strictNullChecks mode. Improvements to intersection and union types help TypeScript narrow its type definitions.
Also in TypeScript 4.8, the TypeScript transpiler can better infer types within pattern string types.
Improvements to the TypeScript transpiler with –build, –watch, and –incremental reduce typical transpilation times by 10-25%.
These two versions added dozens of other improvements and bug fixes. Read the full release notes to learn more about each release.
The official release of TypeScript 4.8 is expected in mid-to-late August, just in time for TypeScript turning 10 in October!
TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged through the TypeScript GitHub project and must follow the TypeScript contribution guidelines and the Microsoft Open Source Code of Conduct.
Add Comment