TypeScript Concepts
Contravariance — reversing type direction
A function accepting a broad input can serve a caller that supplies only a narrower input. With strictFunctionTypes, ordinary function parameter positions are checked contravariantly, subject to special cases; method declarations retain a bivariance exception. Return types are checked separately. Observed output: 'Pip'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.
Formal Syntax & Structure
typescript syntaxtypescript
type Consumer<T> = (value: T) => void;Working Example
Example Snippettypescript
type Animal = { name: string };type Dog = Animal & { bark(): void };const report = (a: Animal) => console.log(a.name);const reportDog: (d: Dog) => void = report;reportDog({ name: "Pip", bark() {} });