AnatomyOf

Anatomy of a TypeScript file

example.ts
1interface Shape {
2 area(): number
3}
4
5class Circle implements Shape {
6 constructor(private radius: number) {}
7 area(): number {
8 return Math.PI * this.radius ** 2
9 }
10}
11
12// any would compile too, but then what were we even doing here
13function describeArea<T extends Shape>(shape: T): string {
14 return 'Area: ' + shape.area().toFixed(2)
15}
16const shapes: Shape[] = [new Circle(2), new Circle(5)]
17console.log(shapes.map(describeArea))
TypeScript is a strict syntactical superset of JavaScript that adds optional static typing, designed for development of large applications and transpiles to JavaScript.