isolatedModules and safe single-file transpilation
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Explain what isolatedModules requires and why bundlers need it.
- Recognize constructs that fail under single-file transpilation.
- Use export type, const objects and explicit exports to stay compatible.
Overview
#Why the flag exists
Tools like esbuild, SWC and Babel transpile one file at a time without the type checker. isolatedModules makes the checker reject constructs those tools cannot handle safely.
What breaks
- Re-exporting a type without
export type:export { User } from "./models";is ambiguous. - A
const enummember is inlined by the checker, which a single-file transpiler cannot do. - A file with no imports or exports is treated as a script, so its declarations become global.
The fixes
export type { User } from "./models";
Use a const object instead of a const enum. Add export {} to a file that should be a module.
verbatimModuleSyntax
This newer flag is stricter: it keeps imports and exports exactly as written and forbids eliding them, so import type becomes mandatory for types. It subsumes the main isolatedModules concerns.
Practical rule
If a build uses esbuild or SWC, enable isolatedModules. Prefer import type and export type, avoid const enums, and never rely on a file being a script by accident.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
With `isolatedModules` on, does this compile when `User` is an interface? ```ts export { User } from "./models"; ```
Exercise 2Implement
Fix this re-export so it passes `isolatedModules`. ```ts interface Config { debug: boolean } export { Config }; ```
starter.typescriptinterface Config { debug: boolean } export { Config };Reveal the reference answer
reference.typescriptinterface Config { debug: boolean } export type { Config };Authored expected output: Compile-time: no isolatedModules error. Runtime: no value export for Config.
export type states that the binding is a type, so the transpiler elides it safely.
Exercise 3Explain
Why do bundlers require isolatedModules, and which two constructs most often break?
Reveal the reference answer
reference.typescriptBundlers transpile each file in isolation without the type checker, so they cannot know whether a binding is a type or a value. Type re-exports without export type and const enum members both require type information to emit correctly, so isolatedModules rejects them.Authored expected output: A written explanation naming single-file transpilation, type re-exports and const enums.
The flag exists because the transpiler has less information than the checker.
Common misconceptions
#- isolatedModules runs the type checker per file.
- const enums are fine with any bundler.
- A file without imports is always a module.
Follow-up practice
#- typescript-variance
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.