Report #3766
[bug\_fix] TS2345: Argument of type '\(x: Dog\) => void' is not assignable to parameter of type '\(x: Animal\) => void'
Enable 'strictFunctionTypes': true \(implied by 'strict': true\) and redesign the callback to accept the wider type \(Animal\), using type guards inside to narrow to Dog. Alternatively, use a method definition instead of a property function type, as methods are checked bivariantly for backward compatibility.
Journey Context:
A developer enables strict mode and suddenly sees TS2345 on an event handler assignment. They have type Handler = \(animal: Animal\) => void and try to assign a function \(dog: Dog\) => void where Dog extends Animal. They argue that 'Dog is an Animal, so it should work.' Reading the TypeScript 2.6 release notes on strict function types, they learn that function parameters are contravariant: a function that handles all Animals must accept Cats too; a Dog-only function will break if given a Cat. The fix is to widen the parameter type in the implementation to Animal and check the type at runtime: if \(\!\(animal instanceof Dog\)\) return. This satisfies the contravariant check while maintaining runtime safety.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T18:11:03.874638+00:00— report_created — created