TypeScript Version: 2.4.1
Code
Case 1
// overloaded functionsdeclarefunctionf(a: number): number;declarefunctionf(a: string): string;functiong<Textendsnumber|string>(a: T){returnf(a);// either of arguments is acceptable// ~// Argument of type 'T' is not assignable to parameter of type 'string'.// Type 'string | number' is not assignable to type 'string'.// Type 'number' is not assignable to type 'string'.}Case 2
declarefunctionf2<T>(a: Promise<T>): number;declarefunctionf2<T>(a: T): string;functiong2<T>(a: T){constresult: string=f2(a);// first overload is ignored, but the argument can be a Promisereturnresult;}Expected behavior:
Case 1: compiles with no errors
Case 2: error that type string | number is not assignable to type string
Actual behavior:
Case 1: error
Argument of type 'T' is not assignable to parameter of type 'string'.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'
Case 2: compiles without errors
Note
I remember here were some discussions about that, so that verifying all possible paths may result in N*M complexity (N overloads, M constituent types in unions). I could not find it.
The second case seems unsafe at all, because skips a possibly valid overload which may effect on return type. I expect that f2(a) would be of type number | string because either of these two overloads can play. It actually has the same result with a: any.
TypeScript Version: 2.4.1
Code
Case 1
Case 2
Expected behavior:
Case 1: compiles with no errors
Case 2: error that type
string | numberis not assignable to typestringActual behavior:
Case 1: error
Case 2: compiles without errors
Note
I remember here were some discussions about that, so that verifying all possible paths may result in N*M complexity (N overloads, M constituent types in unions). I could not find it.
The second case seems unsafe at all, because skips a possibly valid overload which may effect on return type. I expect that
f2(a)would be of typenumber | stringbecause either of these two overloads can play. It actually has the same result witha: any.