TypeScript Concepts
Invariance — neither direction is safe
A type that both produces and consumes T may be invariant: neither subtype direction is generally assignable. Under strict function checking a read/write cell demonstrates this. Method bivariance and other structural exceptions mean the variance of a real generic depends on its members. Observed output: '4'. 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 Cell<T> = { read: () => T; write: (v: T) => void };Working Example
Example Snippettypescript
type Cell<T> = { read: () => T; write: (v: T) => void };let count = 1;const cell: Cell<number> = { read: () => count, write: n => { count = n; } };cell.write(4);console.log(cell.read());