TypeScript - Functions, Parameters, Overloads, this
Function types
type Binary = (a: number, b: number) => number;
const add: Binary = (a, b) => a + b;
Optional/Default/Rest
function greet(name: string, suffix = '!'): string { return `Hello, ${name}${suffix}` }
function log(msg?: string) { console.log(msg ?? '(none)') }
function sum(...xs: number[]) { return xs.reduce((a,b)=>a+b,0) }
Overloads
function len(x: string): number;
function len(x: any[]): number;
function len(x: string | any[]) { return x.length }
this
interface Counter { count: number; inc(this: Counter): void }
const c: Counter = { count: 0, inc() { this.count++ } }
Call/Construct Signatures
interface DateFactory { new (ms: number): Date; (iso: string): Date }
const make: DateFactory = (x: any) => new Date(x);