ProgramGuru

Exporting Variables, Functions, and Classes

Exporting Variables, Functions, and Classes — the core idea

The export keyword exposes selected declarations from a module, including constants, functions, and classes. The consumer's import names must match named exports. Export the public API, not incidental private helpers.

What you’ll learn

  • Explain exporting variables, functions, and classes 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: currency, total and Order become named exports. Calling the exported function computes 36, and the class instance supplies id 9. TypeScript checks the constructor parameter's number type.

Keep the boundary clear: export does not freeze values or make exported classes globally available. Consumers still need an import.

See the idea as a flow

Exporting Variables, Functions, and Classes — conceptual flowexport const then export function then export classExporting Variables, Functions, and Classes1export const2export function3export classMatch 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 exporting-variables-functions-classes.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 const currency = "USD";
export function total(price: number, qty: number): number { return price * qty; }
export class Order { constructor(readonly id: number) {} }
console.log(`${new Order(9).id}: ${currency} ${total(12, 3)}`);

Verified output

9: USD 36

Build: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable exporting-variables-functions-classes.ts; run: node exporting-variables-functions-classes.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

currency, total and Order become named exports. Calling the exported function computes 36, and the class instance supplies id 9. TypeScript checks the constructor parameter's number type.

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.

billing.ts

export const unit = "USD";
export function total(p:number,q:number){return p*q;}
export class Bill { constructor(readonly id:number) {} }

main.ts

import {unit,total,Bill} from "./billing"; console.log(new Bill(4).id,unit,total(3,7));

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

4 USD 21

Avoid this common mistake

export does not freeze values or make exported classes globally available. Consumers still need an import.

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

Move Order and total to an exported billing.ts file; import both in main.ts.

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

How does another module use a named export?
Choose one answer. You can try again.

Answer and explanation

By importing it from the exporting module.

currency, total and Order become named exports. Calling the exported function computes 36, and the class instance supplies id 9. TypeScript checks the constructor parameter's number type.

What to remember

The export keyword exposes selected declarations from a module, including constants, functions, and classes.

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: Named and Default Exports →