You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classEventEmitter<ET>{// works with overloads// off<K extends keyof ET>(...args: [K, unknown]): void// off<K extends keyof ET>(...args: [K | unknown]): voidoff<KextendskeyofET>(...args: [K,unknown]|[K|unknown]):void{}}functiononce<ET,TextendsEventEmitter<ET>>(emittingObject: T,eventName: keyofET,callback: (event: unknown)=>void,): void{emittingObject.off(eventName,callback);/* fails*/// type assertion works as wellemittingObject.off(eventNameaskeyofET,callback);}
π Actual behavior
The call emittingObject.off(eventName, callback); fails with Argument of type '[string | number | symbol, (event: unknown) => void]' is not assignable to parameter of type '[unknown] | [keyof ET, unknown]'.
π Expected behavior
The call should succeed as it did in 4.2
The text was updated successfully, but these errors were encountered:
It looks like something's gone wrong here where we construct a contextual type for argument position 0 (correct) and then decide to use the presence of that to devolve eventName from keyof ET to its constraint string | number | symbol (bad). But it's something specific to the tuple syntax, since equivalentish forms like off<K extends keyof ET>(args: K | boolean, a2: string | number):void {} don't trigger this.
This is caused by #43183. The issue is that the contextual type of the first argument to off is K | unknown, which ends up reducing to just unknown. Since unknown contains no generic types we think it is safe to reduce keyof ET to its constraint string | number | symbol which would be a candidate for narrowing by CFA. The fix is to ensure K | unknown doesn't get reduced. We do that elsewhere for contextual types, just not in the particular scenario of arguments to rest parameters with union types.
Bug Report
π Search Terms
tuples rest parameter regression
π Version & Regression Information
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
The call
emittingObject.off(eventName, callback);
fails withArgument of type '[string | number | symbol, (event: unknown) => void]' is not assignable to parameter of type '[unknown] | [keyof ET, unknown]'.
π Expected behavior
The call should succeed as it did in 4.2
The text was updated successfully, but these errors were encountered: