4 min read
•Question 31 of 48easyHow do Module Types work in TypeScript?
Importing and exporting types.
What You'll Learn
- Exporting types
- Importing types
- Type-only imports
Exporting Types
code.tsTypeScript
// types.ts
export interface User {
id: string;
name: string;
}
export type Status = "active" | "inactive";
// Can also export inline
export type { User, Status };Importing Types
code.tsTypeScript
// app.ts
import { User, Status } from "./types";
const user: User = {
id: "1",
name: "John"
};Type-Only Imports
code.tsTypeScript
// Explicitly import only types (removed in JS output)
import type { User } from "./types";
// Inline type modifier
import { type User, createUser } from "./user";Re-exporting Types
code.tsTypeScript
// index.ts
export type { User } from "./user";
export type { Product } from "./product";
// Export everything
export * from "./types";Namespace Imports
code.tsTypeScript
import * as Types from "./types";
const user: Types.User = { ... };