Type-only imports and exports
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
#- Use import type and export type to mark erased imports.
- Explain how type-only imports affect emitted JavaScript and bundlers.
- Combine value and type imports from one module.
Overview
#import type
import type { User } from "./models";
import type is erased completely, so the bundler never keeps the module for a type-only use.
Inline type specifiers
import { createUser, type User } from "./models";
The value createUser is kept; User is erased. This is the cleanest way to mix.
export type
export type { User } from "./models";
export type Config = { debug: boolean };
Why it matters
With verbatimModuleSyntax, the compiler keeps imports exactly as written, so a plain import { User } that is only used as a type would emit a real import. import type states the intent and avoids a runtime dependency that could pull in side effects.
Classes are values too
A class is both a value and a type. import type { MyClass } imports only the type, so you cannot use it with new. Import it normally when you need the constructor.
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
Under `verbatimModuleSyntax`, what JavaScript does this emit? ```ts import { type User } from "./models"; let u: User; ```
Exercise 2Implement
Rewrite this import so the type is erased but the function is kept. ```ts import { createUser, User } from "./models"; ```
starter.typescriptimport { createUser, User } from "./models";Reveal the reference answer
reference.typescriptimport { createUser, type User } from "./models";Authored expected output: Compile-time: identical types. Runtime: only createUser is imported.
An inline type specifier marks one binding as type-only while keeping the value import.
Exercise 3Debug
This fails because the class is used as a value after a type-only import. Fix it. ```ts import type { Service } from "./service"; const s = new Service(); ```
starter.typescriptimport type { Service } from "./service"; const s = new Service();Reveal the reference answer
reference.typescriptimport { Service } from "./service"; const s = new Service();Authored expected output: Compile error resolved: 'Service' cannot be used as a value because it was imported using import type. A value import fixes it.
A class imported as a type has no runtime constructor.
Common misconceptions
#- import type is removed only by minifiers.
- A class imported with import type can still be instantiated.
- export type changes the runtime export.
Follow-up practice
#- typescript-isolated-modules
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.