ProgramGuru

Type-Only Imports and Exports

Type-Only Imports and Exports — the core idea

Use import type or export type for symbols needed only by the TypeScript checker. Type-only imports are erased from emitted JavaScript, making the intended runtime dependency explicit. Values such as classes still require regular imports when constructed.

What you’ll learn

  • Explain type-only imports and exports with a concrete use case.
  • Run the example and account for every line of its output.
  • Recognize the difference between compile-time checking and runtime behavior.

Understand the rules and when to use them

Reading the code: Payload exists only for compile-time checking. The emitted JavaScript has send and item but no Payload object. A separate consumer could write `import type { Payload } from "./sender.js"`.

Keep the boundary clear: Do not import a class with import type and then call new Class(): the constructor is a runtime value.

See the idea as a flow

Type-Only Imports and Exports — conceptual flowimport type Payload then compile then no runtime importType-Only Imports and Exports1import type Payload2compile3no runtime importMatch the compiler configuration to the JavaScript environment.
Follow the three stages and identify what happens in TypeScript versus JavaScript.

Run the complete TypeScript example

Save the code as type-only-imports-exports.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.

TypeScript code

export interface Payload { message: string }
export function send(payload: Payload): string { return payload.message.toUpperCase(); }
const item: Payload = { message: "received" };
console.log(send(item));

Verified output

RECEIVED

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable type-only-imports-exports.ts; run: node type-only-imports-exports.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.

Trace the example line by line

Payload exists only for compile-time checking. The emitted JavaScript has send and item but no Payload object. A separate consumer could write import type { Payload } from "./sender.js".

The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.

Build the real multi-file example

This separate mini-project demonstrates real imports, exports, or declarations across files. Create each file in one folder, then compile and run main.js. The example was checked separately from the single-file program.

payload.ts

export type Payload = {label: string};

main.ts

import type {Payload} from "./payload";const data:Payload={label:"ok"}; console.log(data.label);

CommonJS setup: compile with npx tsc --strict --target es2022 --module commonjs main.ts and run node main.js. Compile from the same directory so relative modules are discovered. If this example contains helpers.js, keep that runtime file alongside the emitted main.js.

Verified output from the mini-project

ok

Avoid this common mistake

Do not import a class with import type and then call new Class(): the constructor is a runtime value.

Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.

Practice independently

Create a payload.ts file exporting an interface and a main.ts using import type; inspect the emitted JavaScript.

Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.

Check your understanding

What happens to an import type declaration during JavaScript emission?
Choose one answer. You can try again.

Answer and explanation

It is erased because it does not represent a runtime value.

Payload exists only for compile-time checking. The emitted JavaScript has send and item but no Payload object. A separate consumer could write `import type { Payload } from "./sender.js"`.

What to remember

Use import type or export type for symbols needed only by the TypeScript checker.

Try the practice change and re-run the exact program to test your understanding.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Dynamic Imports →