Sub topic 14
Interop and Speed: `esModuleInterop`, `skipLibCheck`, and `isolatedModules`
Assume you know basic imports and exports, can read tsconfig.json, and understand that TypeScript checks code before JavaScript runs. These three options solve different problems:
esModuleInterophelps CommonJS and ES module imports work together.skipLibCheckreduces checking time by skipping declaration files.isolatedModulesensures each source file can be transpiled safely on its own.
Making CommonJS imports easier
CommonJS modules use module.exports and require. ES modules use export and import. TypeScript can connect the two systems, but their default-import behavior is not identical.
Set esModuleInterop to true when your project imports older CommonJS packages with default-import syntax:
{
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": true,
"strict": true
}
}With this setting, code such as the following can compile more naturally for packages that expose one CommonJS value:
import express from "express";
const app = express();
app.listen(3000);The option changes the generated compatibility helpers and also enables allowSyntheticDefaultImports. The latter affects type checking only. esModuleInterop affects both type checking and emitted JavaScript.
This option does not convert every module into the same format. Your module setting and the package's actual exports still matter. In a modern Node.js project, use NodeNext when you want TypeScript to follow Node's package and file-extension rules.
Skipping library declaration checks
Packages usually include declaration files such as index.d.ts. TypeScript normally checks those files too. Large dependency trees can make this slow, and a third-party declaration file can contain an error that your project cannot fix.
{
"compilerOptions": {
"strict": true,
"skipLibCheck": true
}
}skipLibCheck skips checking the contents of declaration files. TypeScript still checks how your code uses the types exported by those files. It can therefore improve build speed without turning off type checking for your source files.
Use it when dependency declarations are slow or contain unrelated problems. Do not treat it as a solution for incompatible library versions. If two packages declare conflicting global types, skipping checks can hide the conflict until it causes a runtime or build problem.
Requiring independently transpileable files
Some tools transpile TypeScript one file at a time. Examples include fast development transpilers and many build pipelines that use a separate type checker. These tools cannot use information from another file to transform the current file correctly.
Enable isolatedModules to make TypeScript report patterns that require whole-program analysis:
{
"compilerOptions": {
"isolatedModules": true
}
}A common issue is importing a type as if it were a runtime value:
import type { User } from "./models.js";
export function displayUser(user: User): string {
return user.name;
}The import type syntax clearly tells a single-file transpiler that the import should disappear from the emitted JavaScript.
isolatedModules does not replace tsc. It checks whether source files are safe for isolated transformation, while the normal compiler still performs full type checking.
Choosing the settings
| Option | Main benefit | Main risk |
|---|---|---|
| `esModuleInterop` | Easier CommonJS imports | May hide differences between module formats |
| `skipLibCheck` | Faster checks | Can hide declaration conflicts |
| `isolatedModules` | Works with one-file transpilers | Rejects code needing whole-program transformation |
A practical application often enables all three, then keeps a separate full tsc --noEmit check in continuous integration.
Exercise
Which option should you use when a fast transpiler processes each file separately, and what import syntax helps it remove type-only imports?
Show answer
Use isolatedModules, and use import type for imports that exist only for type checking.