This repository contains the sample code used in the variance article.
You can verify TypeScript’s behavior under different variance-related compiler settings.
covariant.ts and invariant.ts intentionally contain type errors for variance testing.
pnpm installRun the following commands to compare strict (theoretical) variance checks with TypeScript’s more permissive behavior:
pnpm exec tsc -p tsconfig.json
pnpm exec tsc -p tsconfig.strict.jsontsconfig.json:strictFunctionTypes: false— shows TypeScript’s bivariant function parameter behaviortsconfig.strict.json:strictFunctionTypes: true— closer to the theoretical, sound variance rules
Use these two modes to observe how arrays, function parameters, and generic types behave differently under each configuration.