Sub topic 14
Interop and speed: esModuleInterop, skipLibCheck, isolatedModules
This chapter covers three compiler options that tend to show up once a project meets real-world packages and build tools. esModuleInterop controls how your code imports older CommonJS packages. skipLibCheck trades some checking for compile speed. isolatedModules makes sure each file can be compiled without looking at the others. It assumes you can read a basic tsconfig.json, know the difference between ES modules (import/export) and CommonJS (require/module.exports), and have seen the module option.
esModuleInterop: importing CommonJS packages
Many npm packages were written for CommonJS. A package like express assigns a single function to module.exports. ES module syntax has no direct match for that. A default import (import express from "express") expects an export named default, and a CommonJS module has no such export.
The esModuleInterop flag bridges the gap. When it is on, the compiler:
- Allows default imports from modules that use
export =ormodule.exports. This is the type-level part, also available separately asallowSyntheticDefaultImports. - Emits small helper functions (
__importDefaultand__importStar) when compiling to CommonJS. These make the default import work at runtime.
Here is the same import with the flag off and on.
import express from "express";
const app = express();
app.get("/", (_req, res) => {
res.send("ok");
});
app.listen(3000);With esModuleInterop off, the compiler reports error TS1259: the module "can only be default-imported using the 'esModuleInterop' flag". With the flag on, the code compiles and runs. The old workaround was import * as express from "express", which stops working once the flag is on. A namespace import is not allowed to be called as a function, because that never worked correctly under the ES module specification.
The runtime gotcha
allowSyntheticDefaultImports alone only changes type checking. It does not change the emitted JavaScript. If you turn it on without esModuleInterop and compile to CommonJS with tsc, the compiler accepts the default import. The emitted code then reads express_1.default, which is undefined, and the program fails when it runs. Enable esModuleInterop when tsc produces your CommonJS output. Enable only allowSyntheticDefaultImports when a bundler or another tool handles the interop and you just need the types to agree.
When you do not need to set it
If module is node16, nodenext, or preserve, esModuleInterop defaults to true. The starter configuration in this tutorial uses "module": "NodeNext", so it already has the flag. For module values such as commonjs, set it explicitly. Setting it everywhere is harmless and makes the intent visible.
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"esModuleInterop": true,
"strict": true,
"outDir": "dist",
"rootDir": "src"
},
"include": ["src"]
}skipLibCheck: faster builds, less checking
A declaration file (.d.ts) describes the types of JavaScript code. Your dependencies ship many of them, and @types packages are made entirely of them. By default, tsc type-checks every one of these files each time, even though you did not write them.
"skipLibCheck": true tells the compiler not to report errors inside any .d.ts file. It still reads those files and uses their types, so your own code is still checked against them. Only errors inside the declaration files are hidden. The usual result is a noticeably shorter compile, and it removes errors you cannot fix yourself, such as two packages that pull in conflicting versions of the same @types package.
The cost is that mistakes in declaration files you wrote yourself also go unreported. This matters for a library that publishes its own .d.ts files. If you ship declarations, check them at least once in continuous integration with skipLibCheck set to false, for example in a separate config that extends your main one.
isolatedModules: files that compile on their own
Full tsc compilation can look across the whole program. It can tell that an imported name is only a type and remove that import from the output. Many other tools cannot do that. Babel, esbuild, swc, and ts.transpileModule convert one file at a time, with no knowledge of the other files.
"isolatedModules": true makes the compiler report any code that would be unsafe for such a tool. It does not change the output. It only adds errors.
The most common error comes from re-exporting a type.
// types.ts
export interface User {
name: string;
}
// index.ts
export { User } from "./types";Under isolatedModules, the re-export in index.ts fails with TS1205: re-exporting a type requires export type. A single-file tool cannot tell whether User is a type or a value, because it never opens types.ts. It might leave an export { User } in the output that fails at runtime. The fix states the intent explicitly:
export type { User } from "./types";The same idea applies to imports. Use import type { User } from "./types" for names that exist only as types. The related verbatimModuleSyntax option (TypeScript 5.0 and later) is stricter. It requires import type for type-only imports and keeps every other import exactly as written. Many projects that use a bundler turn on one of the two.
Other things isolatedModules rejects include ambient const enum declarations, because their values cannot be inlined without reading the declaring file.
Choosing settings
| Option | What it affects | Typical choice |
|---|---|---|
| `esModuleInterop` | Default imports of CommonJS packages, and emitted helpers | `true` when compiling to CommonJS with `tsc` |
| `skipLibCheck` | Whether errors inside `.d.ts` files are reported | `true` for applications, `false` in a CI check for published libraries |
| `isolatedModules` | Extra errors for code that single-file tools cannot handle | `true` with Babel, esbuild, swc, or Vite |
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"isolatedModules": true
}
}Common mistakes
- Fixing a TS1259 error by switching to
import * as liband then callinglib(). This breaks as soon asesModuleInteropis turned on. - Enabling only
allowSyntheticDefaultImportsand compiling withtscto CommonJS, which produces a default import that isundefinedat runtime. - Turning on
skipLibCheckto silence an error in your own.d.tsfile instead of fixing it. - Expecting
isolatedModulesto change emitted JavaScript. It only adds checks.
Exercise
A project compiles with tsc to CommonJS. The config has "module": "commonjs" and "allowSyntheticDefaultImports": true, but not esModuleInterop. The line import express from "express" type-checks, yet running the program throws "express is not a function". Why, and what is the fix?
Show answer
allowSyntheticDefaultImports only affects type checking. The emitted CommonJS reads express_1.default, which is undefined because the package assigns the function to module.exports and has no default property. Setting "esModuleInterop": true makes tsc emit the __importDefault helper, so the default import resolves to module.exports and the call works.
Exercise
With isolatedModules on, export { Config } from "./config" fails with TS1205, where Config is an interface. Write the corrected line and explain why a single-file tool needs it.
Show answer
export type { Config } from "./config";. A tool that compiles one file at a time cannot open config.ts to see that Config is only a type. Without export type, it may emit a runtime re-export of a name that does not exist. The keyword tells the tool to drop the statement entirely.