This Crazy Syntax Lets You Get An Array Element's Type
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
Relative import paths need explicit file extensions in EcmaScript imports when '--moduleResolution' is 'node16' or 'nodenext'.
// 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.
// 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.
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
Learn how to extract the type of an array element in TypeScript using the powerful Array[number]
trick.
Learn how to publish a package to npm with a complete setup including, TypeScript, Prettier, Vitest, GitHub Actions, and versioning with Changesets.
Enums in TypeScript can be confusing, with differences between numeric and string enums causing unexpected behaviors.
Is TypeScript just a linter? No, but yes.
It's a massive ship day. We're launching a free TypeScript book, new course, giveaway, price cut, and sale.
Learn why the order you specify object properties in TypeScript matters and how it can affect type inference in your functions.