Build with:
$ tsc *.ts
| *.js |
| interface Bounds { | |
| readonly w: number, | |
| readonly h: number, | |
| area(): number, | |
| } | |
| type Circle = Bounds & { | |
| readonly r: number, | |
| } | |
| function circle (r: number) { | |
| const area = () => Math.PI * r * r; | |
| return { r, h: r + r, w: r + r, area }; | |
| } | |
| const mapper = <T, U>(f: (t: T) => U) => | |
| (ts: T[]) => ts.map(f); | |
| const circleMapper = mapper<number, Circle>(circle); | |
| const circles = circleMapper([1, 2, 3]); | |
| interface Bounds { | |
| readonly w: number, // width | |
| readonly h: number, // height | |
| area(): number, | |
| } | |
| function square (w: number, h: number) { | |
| const area = () => w * h; | |
| return { h, w, area }; | |
| } | |
| function perimeter (b: Bounds) { | |
| return 2 * (b.w + b.h); | |
| } | |
| console.log(perimeter(square(2, 4))); |
| type Point = { | |
| x: number, | |
| y: number, | |
| }; | |
| const add = | |
| (p1: Point, p2: Point): Point => ({ | |
| x: p1.x + p2.x, | |
| y: p1.y + p2.y, | |
| }); | |
| const p: Point = { x: 1, y: 1 }; | |
| const o: Point = { x: 2, y: 3 }; | |
| console.log(add(p, o)); | |