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
/* overload #1 */functionf0(fn: ()=>void): (()=>void);/* overload #2 */functionf0<P1extends{}>(fn: (p1: P1)=>void): ((p1: P1)=>void);/* overload #3 */functionf0<P1extends{},P2extends{}>(fn: (p1: P1,p2: P2)=>void): ((p1: P1,p2: P2)=>void);functionf0(fn: (...args: unknown[])=>void): (...args: unknown[])=>void{fn();// Creates a bug in overloads #2 and #3, given both p1 and p2 will be undefined, even though P1 and P2 do not allow it.fn(1);// Should only be valid if P1 extends number; in addition p2 will be undefined, even though P2 does not allow it.fn(1,2);// Should only be valid if P1 and P2 both extend number.return()=>{};}f0((a: string)=>{console.log('a: ',a,'typeof a: ',typeofa)})// Prints: "a: ", undefined, "typeof a: ", "undefined"
π Actual behavior
TS compiles the code above as if it were correct, even though the following code correctly triggers an error for i and j:
But fail to detect it in the presence of generic functions.
Additional information about the issue
In the following similar code, but using a single tuple parameter, TS does detect the potencial error, triggering the "This overload signature is not compatible with its implementation signature" message:
Overloads are checked more loosely than they "should" be, see #13235 among others. You'll probably always be able to find some inconsistency in exactly what is allowed versus rejected.
Joe and Andrew, I agree that it's virtually impossible for TS to cover all use cases. However, as TS already detects the error in the case below (Playground):
I don't see why it fails to detect the same situation when generic types are present, like the similar code below, considering that string is (sort of) a "subset" (i.e. contained in) of the generic type P1 and boolean is a "subset" of P2 (Playground):
π Search Terms
function overloading generic functions invalid code
π Version & Regression Information
β― Playground Link
https://tsplay.dev/N5Y35N
π» Code
π Actual behavior
TS compiles the code above as if it were correct, even though the following code correctly triggers an error for
i
andj
:π Expected behavior
TS should warn about the potencial error.
Notice how TS detects an error in a similar situation, when the functions are NOT generic:
But fail to detect it in the presence of generic functions.
Additional information about the issue
In the following similar code, but using a single tuple parameter, TS does detect the potencial error, triggering the "This overload signature is not compatible with its implementation signature" message:
It makes sense, since these expressions are all invalid:
The text was updated successfully, but these errors were encountered: