The TypeScript 5.3 Feature They Didn't Tell You About
TypeScript 5.3 introduces relaxed rules around readonly arrays and improvements in const type parameters.
Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'.
ts
// Relative import paths need explicit file extensions in// EcmaScript imports when '--moduleResolution' is 'node16'// or 'nodenext'.import { example } from "./foo";
Adding a .ts
extension to the import path doesn't work, and results in the following error:
An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.
ts
// An import path can only end with a '.ts' extension when// 'allowImportingTsExtensions' is enabled.import { example } from "./foo.ts";
Add the .js
extension to the import path.
ts
import { example } from "./foo.js";
This error happens because you've specified moduleResolution: NodeNext
. This tells TypeScript that you want your imports and exports to conform strictly to the Node spec.
The Node spec requires that you use .js
file extensions for all imports and exports. This was decided so that a relative import path like ./foo.js
would work both in Node and the browser.
This also simplifies Node's module resolution strategy - Node doesn't have to do any guesswork to figure out what file to import. Thanks to Gil Tayer for clarifying this for me.
tsconfig.json
to use moduleResolution: Bundler
instead of moduleResolution: NodeNext
.Share this article with your friends
TypeScript 5.3 introduces relaxed rules around readonly arrays and improvements in const type parameters.
Learn how to provide a TypeScript playground when asking for help with your TypeScript questions, making it easier for others to assist you.
Learn how to work with events in React and TypeScript, from onClick to onSubmit.
A step-by-step guide on setting up ESBuild to bundle a Node application.
Learn how to add TypeScript to your existing React project in a few simple steps.
Learn the essential TypeScript configuration options and create a concise tsconfig.json file for your projects with this helpful cheatsheet.