0Pricing
TypeScript Academy · Lesson

ES Module Syntax: import and export

Use named and default exports in TypeScript files.

ES Module Syntax: import and export is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome

TypeScript supports ES module syntax for organizing code into separate files. This lesson covers named exports, default exports, and imports.

Named Exports

Use `export` to make a declaration available to other files.
// math.ts
export function add(a: number, b: number): number { return a + b; }
export const PI = 3.14159;
export interface Config { host: string; }

Named Imports

Import specific named exports using curly braces.
// app.ts
import { add, PI } from './math';
console.log(add(1, 2)); // 3
console.log(PI);        // 3.14159

Default Exports

A module can have one default export. Import it without curly braces and with any name.
// greeter.ts
export default function greet(name: string): string {
  return 'Hello, ' + name;
}

// app.ts
import greet from './greeter';
console.log(greet('Alice'));

Import Aliases

Use `as` to rename an import to avoid naming conflicts.
import { log as consoleLog } from './logger';
import { User as UserModel } from './models';

Namespace Imports

Import all exports as a namespace object.
import * as math from './math';
console.log(math.add(1, 2));
console.log(math.PI);

Re-Exporting

Re-export from another module to create a public API surface.
// index.ts
export { add, PI } from './math';
export { greet } from './greeter';
export * from './utils';

Type-Only Exports

Export types without including runtime code using `export type`.
// types.ts
export type { User, Product };
export type UserId = string;

Type-Only Imports

`import type` ensures no runtime code is included for the import. Useful for importing interfaces and type aliases.
import type { User } from './models';
// Only the type is imported — no JavaScript
function greet(user: User): string { return user.name; }

Module Resolution

TypeScript resolves imports based on the `moduleResolution` tsconfig setting. `bundler` or `node16` are recommended for modern projects.

Importing JSON

Enable `resolveJsonModule: true` in tsconfig to import JSON files with type inference.
// tsconfig.json: "resolveJsonModule": true
import config from './config.json';
console.log(config.host); // typed based on JSON structure

Quick Check

Which import syntax ensures the import produces no JavaScript output (type-only)?

Recap

Use named exports/imports for multiple items, default exports for a module's main item, and import type for types. Re-exports in index.ts create clean public APIs.

Frequently asked questions

Is the “ES Module Syntax: import and export” lesson free?

Yes — the full text of “ES Module Syntax: import and export” is free to read here on the web, and the TypeScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “ES Module Syntax: import and export”?

Use named and default exports in TypeScript files. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start TypeScript Academy?

No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ES Module Syntax: import and export” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this TypeScript Academy lesson?

Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. ES Module Syntax: import and export
  2. Re-exports and Barrel Files
  3. Type-Only Imports and Exports
  4. Namespaces vs Modules: Modern Usage
← Back to TypeScript Academy