TypeScript
tsx runs TypeScript without checking types. It removes type syntax and transforms code for execution, so a program can run even when TypeScript reports a type error. tsx does not require the typescript package to be installed.
Use TypeScript's tsc command separately to check types. This page sets up a workflow with two independent commands: one to run your code, and one to check it.
Development workflow
Run your program with tsx while developing. Editors such as VS Code can show type errors as you edit, without blocking execution.
For projects, also run a whole-project type check before accepting changes, for example in CI. Editor diagnostics do not replace a repeatable project check.
Installation
In a project with a package.json, install tsx, TypeScript, and Node.js type definitions. Check the Node.js requirements and TypeScript compiler requirements first. Run all commands on this page from the project root.
npm install -D tsx typescript @types/nodepnpm add -D tsx typescript @types/nodeyarn add -D tsx typescript @types/nodetypescript provides tsc. @types/node describes Node.js APIs for the type checker; choose a version appropriate for the Node.js version your project supports.
tsconfig.json
tsconfig.json configures the TypeScript project. tsx also reads supported options from this file, but it does not run the TypeScript compiler.
Recommendation
Create this configuration file in the project root:
tsconfig.json
{
"compilerOptions": {
// Treat files as modules even without import/export
"moduleDetection": "force",
// Preserve module syntax and use bundler-style resolution
"module": "Preserve",
// Check types without emitting JavaScript; tsx runs the source
"noEmit": true,
// Allow explicit TypeScript extensions in imports
"allowImportingTsExtensions": true,
// Allow JSON and JavaScript imports
"resolveJsonModule": true,
"allowJs": true,
// Enable TypeScript's interoperability checks for CommonJS imports
"esModuleInterop": true,
// Check compatibility with single-file transformation
"isolatedModules": true
}
}This is a starting configuration for checking code run with tsx, not a configuration for emitting a Node.js build; see the TypeScript compiler requirements. Add other checking options, such as strict, according to your project's needs.
module: "Preserve" does not select the runtime module format; see CommonJS and ES modules.
Consider enabling verbatimModuleSyntax to require explicit type-only imports and exports. Existing code may need changes such as import type { User } from './user'. See TypeScript's type-only imports and exports reference.
JSX
For .jsx and .tsx files, tsx respects these JSX transformation options:
The .tsx extension allows JSX syntax; it does not select or install a JSX library for you.
Type checking
Create this script in the project root:
example.ts
const name: string = 'world'
console.log(`Hello, ${name}!`)package.json script
Add these scripts to your existing package.json:
package.json (add to existing scripts)
{
"scripts": {
"start": "tsx ./example.ts",
"type-check": "tsc"
}
}Run them from the project root:
npm start
npm run type-checkpnpm start
pnpm type-checkyarn start
yarn type-checkThe first command prints:
Hello, world!The type check exits successfully without output and does not write JavaScript files because the recommended configuration enables noEmit.
To see the difference, change the declaration to const name: number = 'world' and run both scripts again. tsx still prints the same greeting, but the type check exits unsuccessfully with this diagnostic excerpt:
error TS2322: Type 'string' is not assignable to type 'number'.Pre-commit hook
Run the same type-check script in CI or, optionally, in a Git hook. For hook installation and configuration, see simple-git-hooks. Keep the project check independent of tsx so running code does not depend on type-checking success.
Custom tsconfig.json path
By default, tsx detects tsconfig.json from the current working directory. To choose a different file, copy the configuration above to tsconfig.custom.json and pass --tsconfig before the script path:
npx tsx --tsconfig ./tsconfig.custom.json ./example.tsWith the original string declaration in example.ts, the command prints:
Hello, world!This selects the configuration for tsx, not for tsc. To check the same project configuration, change the type-check script to tsc --project ./tsconfig.custom.json and run it as above.
Compiler limitations
tsx uses esbuild to transform TypeScript. This transformation is separate from TypeScript's type checker:
- Only supported
tsconfig.jsonoptions affect transformation. Type-checking options do not make tsx check types. emitDecoratorMetadatais not supported because it requires type information.- Direct
eval()can behave differently after transformation; do not rely on it to access local variables by their original names.
See esbuild's JavaScript caveats and TypeScript caveats for the transformation constraints. To produce JavaScript files for other users or deployment tools, see Build for distribution.
Troubleshooting
If tsc rejects a configuration option or its value, check the installed compiler against the TypeScript compiler requirements. Run the project's type-check script so it uses the locally installed compiler rather than a global installation.
