// sample-shapes.mts: an ES module written in TypeScript. // Free sample from 99helpers.com/file-types/ts // .mts means "always an ES module", like .mjs for JavaScript. // sample-main.mts imports from this file: keep both in the same folder. // Exported types: other files can use them, and they vanish from the // JavaScript output. export type Point = { x: number; y: number }; export type Shape = | { kind: "circle"; centre: Point; radius: number } | { kind: "rectangle"; corner: Point; width: number; height: number }; // Exported values: these exist when the code runs. export const UNIT = "cm"; export function area(shape: Shape): number { // `kind` tells the shapes apart, so each case knows its own fields. switch (shape.kind) { case "circle": return Math.PI * shape.radius ** 2; case "rectangle": return shape.width * shape.height; } }