Skip to the solution
MediumFrontend9 min read

How does allowImportingTsExtensions work with moduleResolution: "bundler" and .ts imports?

252 views

The problem

Question presented to candidate: "Some projects write import { twice } from \"./math.ts\" with the .ts extension. What does allowImportingTsExtensions actually allow, why does tsc insist on noEmit or emitDeclarationOnly with it, and when is this a good idea?"

What a strong answer should cover:

  • Without the flag, a value import ending in .ts, .mts or .cts is TS5097; with it, tsc accepts the path and type-checks against the source file
  • tsc never rewrites import paths (before 5.7), so emitted JavaScript would keep "./math.ts" and fail to load; that is why the flag requires noEmit or emitDeclarationOnly (TS5096)
  • It fits setups where something else runs or builds the .ts files: Vite or esbuild with moduleResolution: "bundler", Bun, Deno, and Node.js type stripping
  • import type with a .ts path is allowed even without the flag; importing a .d.ts path as a value is TS2846
  • Emitted .d.ts files keep the .ts specifiers; .js specifiers still work alongside
  • TypeScript 5.7 added rewriteRelativeImportExtensions, which rewrites ./math.ts to ./math.js in the output, for projects that do want tsc to emit JavaScript

Code / implementation expected: Optional β€” a tsconfig with the flag and noEmit, and an import that needs it.

  • moduleResolution
  • bundler

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

9 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

TypeScriptWhat tsc says and emits for .ts import paths

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 57 of 81 in TypeScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

What does the satisfies operator do?

Continue