Report #38463
[bug\_fix] TS2684: The 'this' context of type 'void' is not assignable to method's 'this' of type 'MyClass'
With strictFunctionTypes enabled, methods in classes have a specific 'this' context that must match when passed as callbacks. Either convert the method to an arrow function property \(handle = \(\) => \{ ... \}\) to lexically bind 'this', explicitly bind it in the constructor \(this.handle = this.handle.bind\(this\)\), or change the interface's callback definition to expect a 'this' of 'void' by adding 'this: void' as the first parameter in the callback signature.
Journey Context:
Developer defines a class with a method intended as an event handler: 'class Handler \{ onClick\(e: Event\) \{ console.log\(this.id\); \} \}'. They pass it to an event emitter: 'emitter.on\('click', handler.onClick\)'. With strict mode on, TypeScript errors that 'this' is void in the callback context but the method expects 'MyClass'. Developer tries 'handler.onClick.bind\(handler\)' which works but is verbose. They try 'const fn = handler.onClick' which also fails. The insight is that methods in TypeScript have a bivariant 'this' parameter by default, but strictFunctionTypes enforces contravariance. The cleanest fix is using an arrow function property which captures 'this' lexically, making the property a field rather than a method on the prototype, satisfying the function type check.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T19:02:16.522262+00:00— report_created — created