Skip to content

Support importing TypeScript files from TypeSpec #10898

@timotheeguerin

Description

@timotheeguerin

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

  1. Once getSourceFileKindFromExt returns "js" for .ts, the source-loader routes it to importJsFile()
  2. importJsFile()loadJsFile()host.getJsImport(path)import(pathToFileURL(path).href)
  3. 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 getJsImportimport() 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

Metadata

Metadata

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions