Type extraction
interface Building { room: { door: string, walls: string[], }; } type Walls = Building['room']['walls']; // string[]
Modules
export interface User { ... }
Generics
class Greeter<T> { greeting: T constructor(message: T) { this.greeting = message } } let greeter = new Greeter<string>('Hello, world')
Classes
class Point { x: number y: number static instances = 0 constructor(x: number, y: number) { this.x = x this.y = y } }
Inheritance
class Point {...} class Point3D extends Point {...} interface Colored {...} class Pixel extends Point implements Colored {...}
Short fields initialisation
class Point { static instances = 0; constructor( public x: number, public y: number, ){} }
Fields which do not require initialisation
class Point { public someUselessValue!: number; ... }
Function types
interface User { ... } function getUser(callback: (user: User) => any) { callback({...}) } getUser(function (user: User) { ... })
Type aliases
type Name = string | string[]
[Interfaces] Dynamic keys
{ [key: string]: Object[] }
[Interfaces] Read only
interface User { readonly name: string }
[Interfaces] Optional properties
interface User { name: string, age?: number }
[Interfaces] Explicit
interface LabelOptions { label: string } function printLabel(options: LabelOptions) { ... }
Reference
Top comments (1)
For Generics, in this example, specifying is useless because TypeScript will infer it