TypeScript Reference ยท tsconfig: Language, Modules and Emit Options
TypeScript allowImportingTsExtensions option
Reference for allowImportingTsExtensions: import paths ending in .ts, the noEmit requirement, errors TS5096 and TS5097, and the Node type-stripping use case.
allowImportingTsExtensions lets an import path end in a TypeScript extension such as ./math.ts, ./view.tsx or ./util.mts. Without it, the compiler rejects those paths, because by default you are expected to write the extension of the file that will exist after compilation (./math.js) or no extension at all.
The option exists for tools that run your .ts files directly: Node's built-in type stripping, Deno, Bun and bundlers. In those tools the file on disk really is math.ts, so the import has to say so. Because a compiled .js file would then import a file that does not exist, TypeScript only allows the option when it is not going to write JavaScript for you.
{ "compilerOptions": { "allowImportingTsExtensions": true, "noEmit": true } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
{ "compilerOptions": { "allowImportingTsExtensions": true } }
| Part | Description |
|---|---|
| Value | true or false. Default: false. |
| Requires | One of noEmit, emitDeclarationOnly or rewriteRelativeImportExtensions. Otherwise: TS5096: Option 'allowImportingTsExtensions' can only be used when either 'noEmit' or 'emitDeclarationOnly' is set. |
| Error when off | TS5097: An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled. |
| Affects | import, export ... from, dynamic import() and import x = require() with a relative path. |
Edge cases: declaration files cannot be imported by their own extension (./types.d.ts is still an error); write ./types.js or ./types. The option does not change how files are found, only which extensions are accepted in the text of an import.
Usage notes
- The usual partner is
noEmit. Let a runtime or bundler execute the.tsfiles and usetsc --noEmitonly for checking. See noEmit. - If you must emit, switch on rewriteRelativeImportExtensions (TypeScript 5.7 and later). The compiler then rewrites
./math.tsto./math.jsin the output, so one source tree works both when run directly and when compiled. - Node type stripping needs real extensions in every relative import, which is why this option is part of most "run TypeScript with Node" setups. Pair it with erasableSyntaxOnly so you do not use syntax Node cannot strip.
- Only relative paths are affected. Package names such as
"zod"never carry an extension. - Common mistake: adding the option and then running
tscwith emit. The output would import.tsfiles from.jsfiles and fail at runtime. - Libraries that publish compiled code should usually keep this option off and write
.jsextensions, asnodenextexpects.