Course contentsShow
← TypeScript
Lesson 1090 of 1,79626. Modules in TypeScriptFree lesson

Basic import and export of types

Import and export type aliases and interfaces using standard ES module syntax.

Basic import and export of types

What you'll learn: How to share type aliases and interfaces between modules using TypeScript's standard import and export syntax.


The core idea

Just like you export and import functions, variables, or classes in JavaScript, TypeScript lets you export and import your custom types — both type aliases and interfaces — across different files. This makes it possible to define a type once and reuse it everywhere, keeping your codebase DRY (Don't Repeat Yourself) and consistent.

The syntax is exactly what you already know from ES modules: export in one file, import in another.


Exporting types

You can prefix any type alias or interface with the export keyword:

// types.ts
export type UserId = string;

export interface User {
  id: UserId;
  name: string;
  email: string;
}

Both UserId and User are now available for other modules to import.


Importing types

In another file, you import them just like you'd import a function or constant:

// user-service.ts
import { UserId, User } from './types';

function getUser(id: UserId): User {
  // implementation
  return { id, name: 'Alice', email: 'alice@example.com' };
}

The imported types work exactly as if they were defined locally — full type checking, autocomplete, and all.


Default exports work too

You can also use default exports for types:

// config.ts
type Config = {
  apiUrl: string;
  timeout: number;
};

export default Config;

And import them:

// app.ts
import Config from './config';

const appConfig: Config = {
  apiUrl: 'https://api.example.com',
  timeout: 5000
};

Think of types as blueprints you can ship

Exporting and importing types is like sharing blueprints across different construction sites. Define your data structures once in a central location, then import them wherever you need to ensure everyone's building to the same spec.


Key takeaway: Use standard export and import syntax to share type aliases and interfaces across modules — TypeScript treats types just like values when it comes to module boundaries.