// sample-main.mts: a TypeScript ES module that imports another one. // Free sample from 99helpers.com/file-types/ts // Run: node sample-main.mts (Node.js 22.18+ or 23.6+) // Check: npx tsc --noEmit --allowImportingTsExtensions sample-main.mts // A type-only import. Node.js needs the `type` keyword: types are erased, // so without it Node.js would look for a value called Shape and fail. import type { Shape } from "./sample-shapes.mts"; // A value import. The file extension is written out, as Node.js requires. import { area, UNIT } from "./sample-shapes.mts"; const shapes: Shape[] = [ { kind: "circle", centre: { x: 0, y: 0 }, radius: 1 }, { kind: "rectangle", corner: { x: 2, y: 2 }, width: 3, height: 4 }, ]; // Top-level await: allowed because this file is a module. const total: number = await Promise.resolve( shapes.reduce((sum, shape) => sum + area(shape), 0), ); for (const shape of shapes) { console.log(`${shape.kind}: ${area(shape).toFixed(2)} ${UNIT}²`); } console.log(`total: ${total.toFixed(2)} ${UNIT}²`); console.log(`loaded from: ${import.meta.url}`);