Microsoft announced the launch of the TypeScript 4.7 beta which includes ECMAScript Module Support in Node.js, control over module detection, control-flow analysis for computed properties, and more.
In TypeScript 4.5, Microsoft added nightly-only support for ESM in Node.js to get some feedback from users and let library authors ready themselves for broader support. TypeScript 4.7 expands this functionality with two new ‘module’ settings: ‘node12’ and ‘nodenext’.
The new setting controls whether ‘.js’ files are interpreted as ES modules or CommonJS modules, and defaults to CommonJS when not set.
Also, TypeScript 4.7 beta introduces a new option called ‘moduleDetection’. ‘moduleDetection’ can take on 3 values: “auto” (the default), “legacy” (the same behavior as 4.6 and prior), and “force” to alleviate the ambiguity between existing “script” code and module code.
The “auto” detection will look for ‘import’ and ‘export’ statements and whether the “type” field in ‘package.json’ is set to “module” when running under ‘–module nodenext/–module node12’, and whether the current file is a JSX file when running under ‘–jsx react-jsx’.
TypeScript 4.7 also now analyzes the type of computed properties and narrows them correctly, whereas previously, it would not consider any type guards on ‘obj[key]’, and would have no idea that ‘obj[key]’ was really a ‘string’.
Other new features in TypeScript 4.7 beta include improved function interference in objects and methods, instantiation expressions, and much more. Additional details are available here.