theory.ts

Modules & Namespaces

ES Modules (import, export)

ES Modules allow code to be split into separate files and reused across a project.

  • Uses `import` and `export` to manage dependencies.
  • Supports both named and default exports.
  • Standardised in JavaScript and fully supported in TypeScript.
// Exporting functions (utils.ts)
export function add(a: number, b: number): number {
  return a + b;
}

export function multiply(a: number, b: number): number {
  return a * b;
}

// Importing functions (main.ts)
import { add, multiply } from "./utils";

console.log(add(2, 3)); // 5
console.log(multiply(2, 3)); // 6