TypeScript Support
Node.js provides built-in support for running TypeScript code through type stripping. This allows you to run TypeScript files directly without a separate compilation step.Enabling TypeScript Support
There are two ways to enable TypeScript support in Node.js:1. Full TypeScript Support
For full support of all TypeScript features, includingtsconfig.json, use a third-party package like tsx:
- Full TypeScript feature support
tsconfig.jsonconfiguration- Any TypeScript version compatibility
- Code transformation (enums, namespaces, etc.)
2. Built-in Type Stripping
Node.js includes built-in type stripping that is enabled by default. This provides a lightweight way to run TypeScript:Type Stripping
Type stripping is a lightweight feature that:- Removes TypeScript type annotations
- Replaces types with whitespace
- Does not perform type checking
- Does not transform code
- Does not require source maps
Supported Features
Type stripping supports:- Type annotations
- Interfaces
- Type aliases
- Type-only imports/exports
- Generic type parameters
- Type assertions
- Type-only namespaces
Unsupported Features
Features that require code transformation are not supported:enumdeclarationsnamespacewith runtime code- Parameter properties
- Import aliases
- Decorators (not yet standardized)
Recommended tsconfig.json
For optimal compatibility with type stripping, use these settings:Module System
Node.js supports both CommonJS and ES Modules in TypeScript:File Extensions
.tsfiles - Module system determined like.jsfiles (use"type": "module"inpackage.jsonfor ESM).mtsfiles - Always ES modules (like.mjs).ctsfiles - Always CommonJS (like.cjs).tsxfiles - Not supported
Import/Require Syntax
- ✅
import './file.ts' - ❌
import './file' - ✅
require('./file.ts') - ❌
require('./file')
Type Imports
Use thetype keyword for type-only imports:
verbatimModuleSyntax tsconfig option enforces this pattern.
Examples
Basic TypeScript File
Using Interfaces
Generic Functions
Type Assertions
Working with Node.js APIs
Non-File Inputs
Type stripping works with--eval and STDIN:
- REPL (interactive mode)
--check(syntax checking)inspect(debugger)
Source Maps
Since type stripping only removes types (replacing them with whitespace), source maps are not needed for accurate stack traces. Line numbers in TypeScript files match the stripped JavaScript output.Dependencies and node_modules
Node.js refuses to process TypeScript files insidenode_modules directories. This encourages package authors to publish transpiled JavaScript rather than TypeScript source.
If you need TypeScript in dependencies:
- Publish packages as JavaScript
- Include type definitions (
.d.tsfiles) - Reference types via
@types/packages
Path Aliases
TypeScript’spaths configuration in tsconfig.json is not supported. Instead, use Node.js subpath imports:
#.
Limitations
No tsconfig.json Processing
Node.js does not readtsconfig.json, so features depending on it are unsupported:
- Path mapping (
paths) - Base URL (
baseUrl) - Target transpilation (
target) - JSX transformation
- Module resolution strategies
No Code Transformation
These TypeScript features require code generation and are not supported:Best Practices
-
Use type imports - Always use
import typefor types: -
Include file extensions - Always include
.tsin imports: - Avoid transformation features - Don’t use enums, namespaces with runtime code, etc.
-
Enable strict mode - Use strict TypeScript settings for better type safety:
-
Use @types packages - Install type definitions for external packages:
-
Publish as JavaScript - If creating packages, publish transpiled JS with
.d.tsfiles - Test both modes - Test your code with both type stripping and full compilation
When to Use Each Approach
Use Built-in Type Stripping When:
- Building simple scripts or tools
- Working with modern TypeScript syntax
- Want minimal setup and fast startup
- Don’t need advanced TypeScript features
- Creating Node.js applications (not libraries)
Use Full TypeScript (tsx, ts-node, etc.) When:
- Need enums, namespaces, or decorators
- Require tsconfig.json features
- Building libraries for npm
- Need compatibility with older TypeScript versions
- Want full type checking during development
- Creating browser-targeted code