Sub topic 3
`import type` and `export type`
This chapter assumes you know basic TypeScript types, interfaces, functions, and ordinary JavaScript import and export statements. It also assumes you can run tsc and read a tsconfig.json.
TypeScript has two kinds of module information:
- Runtime values, such as functions, classes, and initialized constants.
- Types, such as interfaces, type aliases, and type annotations.
The JavaScript runtime needs values. It does not need TypeScript-only types. Type-only imports and exports let you state this difference clearly.
Why type-only imports matter
An import type statement imports names only for type checking. TypeScript removes the statement when it emits JavaScript.
Consider these files:
// user.ts
export interface User {
id: number;
name: string;
}
export function createUser(name: string): User {
return { id: Date.now(), name };
}// profile.ts
import { createUser } from "./user.js";
import type { User } from "./user.js";
const user: User = createUser("Ada");
console.log(user.name);The createUser import is needed at runtime because the program calls the function. The User import is needed only by the type checker.
The emitted JavaScript is equivalent to:
import { createUser } from "./user.js";
const user = createUser("Ada");
console.log(user.name);There is no runtime User value to import. An interface exists only during type checking.
Exporting types
Use export type to re-export a type from another module:
// user.ts
export interface User {
id: number;
name: string;
}
export function createUser(name: string): User {
return { id: Date.now(), name };
}// public-api.ts
export { createUser } from "./user.js";
export type { User } from "./user.js";A consumer can then import both kinds of names:
// app.ts
import { createUser } from "./public-api.js";
import type { User } from "./public-api.js";
const user: User = createUser("Ada");
console.log(user);The normal export makes createUser available at runtime. The type export makes User available to TypeScript consumers without creating a JavaScript export for it.
You can also use a type modifier inside a mixed export:
export { createUser, type User } from "./user.js";The separate form is often easier to read when a module has many type exports.
Mixed imports
A module can provide both values and types. You can mark only the type names:
import { createUser, type User } from "./user.js";
const user: User = createUser("Ada");This form is useful when the names come from the same module. It has the same runtime behavior as separating the statements:
import { createUser } from "./user.js";
import type { User } from "./user.js";The namespace form also distinguishes type usage from runtime usage:
import * as userModule from "./user.js";
const user: userModule.User = userModule.createUser("Ada");Whether this works depends on the module's emitted declarations and compiler settings. Separate imports usually make the intent clearer.
Common errors
A frequent mistake is treating a type as a runtime value:
import type { User } from "./user.js";
// Error: User is a type, not a runtime value.
const value = new User();An interface does not produce a constructor. Use a class when you need a runtime object with construction behavior.
Another mistake is using a type-only export for a value:
// Error: createUser is a value, not a type.
export type { createUser } from "./user.js";Use a normal export for createUser.
With strict module settings such as verbatimModuleSyntax, TypeScript expects type names to be marked explicitly. This helps preserve the difference between the source module syntax and the emitted JavaScript.
Practical rule
Ask whether the imported name must exist when JavaScript runs.
- If the code calls it, constructs it, reads it, or passes it as a runtime value, use a normal import.
- If the name appears only in a type annotation, generic, interface extension, or type alias, use
import type. - Use
export typewhen re-exporting a type for other TypeScript files.
Exercise
In import { parse, Config } from "./config.js";, parse is a function and Config is an interface. Rewrite the import so that the emitted JavaScript contains only the runtime import.
Show answer
import { parse, type Config } from "./config.js";