TypeScript Concepts
Covariance — preserving type direction
A covariant type constructor preserves a subtype direction: a producer of a more specific value can be used where a producer of a broader value is expected. Mutable arrays also permit covariant assignments in TypeScript, which can be unsafe. Analyze actual uses of T rather than assuming a generic is covariant. 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 Producer<T> = () => T;Working Example
Example Snippettypescript
type Animal = { name: string };type Dog = Animal & { bark(): void };const makeDog = (): Dog => ({ name: "Pip", bark() {} });const makeAnimal: () => Animal = makeDog;console.log(makeAnimal().name);