Sub topic 12
Using tsc with editors, bundlers, and runners
This chapter assumes you can already compile a project with tsc and read a tsconfig.json. It also assumes you know what strict, noEmit, and outDir do. It covers how tsc sits next to other tools in a real project. Editors, bundlers, and runners each handle only part of the job tsc does.
Two jobs, usually split across tools
tsc does two separate things:
- Type checking. It reads your whole program, resolves imports, and reports type errors.
- Emit. It erases the types and writes JavaScript.
Emit is mechanical, because it only removes annotations and rewrites a few syntax forms. Type checking is the expensive part, because it needs information from every file the program imports.
Most other tools only do the emit step. They strip types file by file and never check them. This is fast, and it is why a type error can pass through a bundler without any warning. The usual setup is to let one tool produce the JavaScript and let tsc --noEmit do the checking.
Editors
Editors do not call tsc. They talk to tsserver, a long-running process that ships inside the typescript package. It keeps the program in memory and answers questions such as "what is the type here?" and "what errors does this file have?". It uses the same checker as tsc, so the errors match.
Some practical points:
- Version. VS Code bundles its own TypeScript and uses it by default. Your project pins another version in
node_modules. If the two differ, the editor can show errors thattscdoes not, or the reverse. Run TypeScript: Select TypeScript Version and choose "Use Workspace Version". You can also set"typescript.tsdk": "node_modules/typescript/lib"in.vscode/settings.json. - Which tsconfig applies. tsserver finds the nearest
tsconfig.jsonabove a file. If the file is not matched by that config'sincludeorfiles, the editor falls back to an inferred project with default options. The result is thatstrictseems to be off in one folder. Tests outsidesrcare the usual cause. - Editor errors are not a gate. Nobody is forced to open the editor, and someone can commit with errors showing. CI must run its own check.
Bundlers and runners transpile only
Tools such as esbuild, swc, Vite, and tsx convert each file on its own. They do not build a whole-program type model, so they cannot report most type errors. Take this file:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet(42));tsc rejects the call greet(42). A transpile-only tool strips the annotations and produces working JavaScript that prints Hello, 42!. The build succeeds, which is the risk of relying on a bundler alone.
The standard fix is two scripts in package.json. One produces output with the bundler. The other checks types with tsc.
{
"name": "bundler-demo",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"typecheck": "tsc --noEmit",
"ci": "npm run typecheck && npm run build"
},
"devDependencies": {
"typescript": "^5.6.0",
"vite": "^6.0.0"
}
}Run npm run typecheck in CI before the build, and many teams also run it as a pre-commit or pre-push hook. Because of noEmit, no .js files are written, so nothing competes with the bundler's output.
A tsconfig for a bundler project
A project built by a bundler needs different options from one compiled by tsc to dist. tsc is only the checker, so options about output, such as outDir, matter little. Options that describe how the bundler resolves modules matter a lot.
{
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2022", "DOM"],
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"noEmit": true,
"isolatedModules": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
},
"include": ["src"]
}What the new options do:
| Option | Effect |
|---|---|
| `moduleResolution: "Bundler"` | Resolves imports the way bundlers do. Relative imports may omit file extensions, and `package.json` `exports` fields are honored. |
| `isolatedModules` | Reports code that cannot be converted correctly one file at a time. |
| `verbatimModuleSyntax` | Keeps imports and exports exactly as written, unless they are marked `type`. Type-only imports must say so. |
| `noEmit` | Writes no files, so the config is safe for pure checking. |
Why isolatedModules and verbatimModuleSyntax matter
A single-file transpiler sees import { User } from "./user" and cannot tell whether User is a type or a value. If it keeps the import and User is only a type, the runtime fails because the export does not exist. If it drops the import and User is a value, code breaks later. With verbatimModuleSyntax, you remove the guess by writing type yourself:
// user.ts
export interface User {
id: number;
name: string;
}
export function makeUser(id: number, name: string): User {
return { id, name };
}// main.ts
import { makeUser } from "./user";
import type { User } from "./user";
const u: User = makeUser(1, "Ada");
console.log(u.name);The import type line is erased completely. The plain import line stays because makeUser is a real value. If you write import { User, makeUser } without type, tsc reports an error under verbatimModuleSyntax, and a bundler cannot silently get it wrong. The same applies to re-exports: use export type { User } from "./user".
Some TypeScript features need cross-file knowledge, so isolatedModules flags them or restricts them. Examples are const enum across files and files with no import or export, which TypeScript would treat as global scripts. Treat these errors as guidance to change the code. They are not a reason to turn the option off.
Runners
A runner executes TypeScript directly, without a separate build step. Three common choices:
| Tool | Checks types? | Notes |
|---|---|---|
| `tsx` | No | Uses esbuild. Fast, and handles ESM and CommonJS. Run with `npx tsx src/index.ts`. |
| `ts-node` | Yes by default, unless `--transpile-only` | Slower because it type-checks. Its ESM setup is more fragile than the others. |
| Node.js itself | No | Recent Node versions can strip types natively. |
Node's built-in type stripping
Node 22.18 and later, and Node 23.6 and later, run .ts files directly by deleting the type annotations. Earlier 22.x releases need the --experimental-strip-types flag. Check your version with node --version, then run:
node src/index.tsNode does not read your tsconfig.json, and it does no type checking. It also only removes syntax that can be erased without changing meaning. TypeScript features that generate JavaScript are rejected. These include enum, namespace with values, and constructor parameter properties. Imports must name the real file, including the .ts extension.
A matching config uses tsc only as a checker and forbids the unsupported syntax:
{
"compilerOptions": {
"target": "ES2023",
"module": "NodeNext",
"strict": true,
"noEmit": true,
"allowImportingTsExtensions": true,
"erasableSyntaxOnly": true,
"verbatimModuleSyntax": true,
"skipLibCheck": true
},
"include": ["src"]
}erasableSyntaxOnly needs TypeScript 5.8 or later. With it, tsc reports an error on any syntax Node could not strip, so you find out at check time and not at runtime. allowImportingTsExtensions lets you write import { x } from "./x.ts", and it requires noEmit or declaration-only output.
When to still emit with tsc
Keep tsc as the emitter when you publish a library. Consumers need .d.ts declaration files, and only tsc generates them with full accuracy. Common arrangements:
tscemits everything, both.jsand.d.ts. This is the simplest option for small libraries.- A bundler emits the
.js, andtsc --emitDeclarationOnly --declarationemits the.d.tsfiles. - An application uses a bundler for output and
tsc --noEmitfor checking.
Common mistakes
- Treating the bundler as the checker. Type errors never surface, because transpilers do not look for them.
- Mixed versions. The editor's TypeScript and the project's TypeScript differ. Select the workspace version.
- Missing
typeon imports. UnderverbatimModuleSyntaxorisolatedModules, the compiler reports an error. Addimport type. - Test files outside
include. The editor uses an inferred project with weaker defaults. Add the folder, or create atsconfigthat extends the base config. - Using
enumwith Node's type stripping. Replace it with a union of string literals or a plain object markedas const.
Summary
Treat tsc --noEmit as the single source of truth for type correctness and let other tools produce JavaScript. The editor gives you early feedback from the same checker. The bundler or runner gives you fast output. Options such as isolatedModules, verbatimModuleSyntax, and erasableSyntaxOnly make tsc reject code that those fast tools would handle wrongly.
Exercise
Your Vite project builds without errors, yet a teammate finds a call that passes a number to a function expecting a string. Which script should have caught it, which tsconfig.json option should that script rely on so it writes no files, and where should it run?
Show answer
A typecheck script running tsc --noEmit should have caught it. Vite only strips types and never reports type errors. The noEmit option keeps tsc from writing JavaScript that would compete with Vite's output. Run it in CI, before or alongside the build, as a required check. An editor or a pre-commit hook can add earlier feedback.
Exercise
A file imports import { User } from "./user", where User is an interface, and verbatimModuleSyntax is enabled. What does tsc report, and how do you fix it?
Show answer
tsc reports an error because User is only a type and the import is not marked as type-only, so a single-file transpiler could not know whether to keep or drop it. Fix it by writing import type { User } from "./user", or import { type User } from "./user" if the same statement also imports values.
Exercise
You run node src/index.ts on a recent Node version and the program fails on a TypeScript enum. Why, and which tsconfig.json option helps you find this earlier?
Show answer
Node's type stripping only deletes annotations. An enum produces runtime JavaScript, so Node cannot erase it. Setting erasableSyntaxOnly (TypeScript 5.8 or later) makes tsc report an error for enums and other non-erasable syntax during type checking. Replace the enum with a union of string literals or an as const object.