Modules and several files

Split a program into .ts files with import and export; no npm packages.

Runs in Zerethon IDE · TypeScript 7 Official TypeScript documentation ↗
main.ts
import { area, type Size } from './geometry';

const size: Size = { width: 3, height: 4 };
console.log(area(size));
geometry.ts
export interface Size {
  width: number;
  height: number;
}

export function area(size: Size): number {
  return size.width * size.height;
}

Output

12
Two files: main.ts (the entry) and geometry.ts. Import without the extension.
  • All .ts files of the project are compiled together, so a type error in any file stops the run.
  • Node's built-in modules and their types are available (fs, path, readline, …).
  • Compiler options are fixed (strict, ES2022, CommonJS output); a tsconfig.json in the project is ignored.
  • npm packages are not available: runs have no network and nothing is installed.

Related topics

Official references