Problem Statement
Currently, the TypeSpec compiler only supports importing .tsp (TypeSpec) and .js/.mjs (JavaScript) files. With Node.js 22.11+ supporting native TypeScript execution without any flag, users should be able to import "./my-decorators.ts"; directly in their .tsp files without a separate build step.
Proposed Solution
The change is minimal — add .ts as a recognized extension in the compiler:
Changes Required
| File |
Change |
packages/compiler/src/core/source-file.ts |
Add .ts → "js" in getSourceFileKindFromExt |
| Tests |
Add tests for .ts file imports mirroring existing .js tests |
Documentation (imports.md) |
Mention .ts support |
How it works
- Once
getSourceFileKindFromExt returns "js" for .ts, the source-loader routes it to importJsFile()
importJsFile() → loadJsFile() → host.getJsImport(path) → import(pathToFileURL(path).href)
- Node.js 22.11+ handles the
.ts import natively via strip-types
What does NOT need to change
module-resolver.ts loadAsFile extensions — only used for extensionless resolution which TypeSpec doesn't use
directoryIndexFiles — not adding index.ts since we're only supporting explicit extensions
node-host.ts getJsImport — import() already handles .ts in supported Node versions
SourceFileKind type — .ts maps to existing "js" kind
- Browser host — no special handling
Design Decisions
- Node version: Keep minimum at Node 22.0.0. If TS import fails on older Node (<22.11), the natural
import() error is clear enough ("Unknown file extension .ts for /path/to/file.ts").
- Extensions: Only
.ts for now (skip .mts/.cts).
- Extensionless resolution: Not applicable — TypeSpec requires explicit extensions in imports.
- Browser host: Let it try and fail naturally (no special handling).
tspMain: No change needed — it's for .tsp files only.
exports interaction: Works naturally — module resolver doesn't filter by extension, just needs getSourceFileKindFromExt to recognize .ts.
- Convention for libraries: No special convention — just document that
.ts works and let users handle their own dev/publish workflow.
Node.js Compatibility
- Node 22.0–22.5: No TS support
- Node 22.6–22.10: Requires
--experimental-strip-types flag
- Node 22.11+: Unflagged, just works (stable)
- Node 23.6+: Also unflagged
Problem Statement
Currently, the TypeSpec compiler only supports importing
.tsp(TypeSpec) and.js/.mjs(JavaScript) files. With Node.js 22.11+ supporting native TypeScript execution without any flag, users should be able toimport "./my-decorators.ts";directly in their.tspfiles without a separate build step.Proposed Solution
The change is minimal — add
.tsas a recognized extension in the compiler:Changes Required
packages/compiler/src/core/source-file.ts.ts→"js"ingetSourceFileKindFromExt.tsfile imports mirroring existing.jstestsimports.md).tssupportHow it works
getSourceFileKindFromExtreturns"js"for.ts, the source-loader routes it toimportJsFile()importJsFile()→loadJsFile()→host.getJsImport(path)→import(pathToFileURL(path).href).tsimport natively via strip-typesWhat does NOT need to change
module-resolver.tsloadAsFileextensions — only used for extensionless resolution which TypeSpec doesn't usedirectoryIndexFiles— not addingindex.tssince we're only supporting explicit extensionsnode-host.tsgetJsImport—import()already handles.tsin supported Node versionsSourceFileKindtype —.tsmaps to existing"js"kindDesign Decisions
import()error is clear enough ("Unknown file extension .ts for /path/to/file.ts")..tsfor now (skip.mts/.cts).tspMain: No change needed — it's for.tspfiles only.exportsinteraction: Works naturally — module resolver doesn't filter by extension, just needsgetSourceFileKindFromExtto recognize.ts..tsworks and let users handle their own dev/publish workflow.Node.js Compatibility
--experimental-strip-typesflag