-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Overloading arrow function return type should allow union type #33482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is correct error. const a: number = foo1(someNum);
const a: string = foo1(someStr); this can be achieved only by a type that is assignable to (a: number | string) => Math.random() > 0.5 ? Number(a) : String(a); then it won't work in case described above. In case of const foo1 : {
(a: number): number;
(a: string): string;
} = (a: number | string): any => a;
^^^ But users will see only 2 correct signatures. |
@IllusionMH My problem is the result is not consistent with For example, function foo3(a: number): number;
function foo3(a: string): string;
function foo3(a: number | string): number[] | string {
return a;
} It shows
The error is on the overload signature. However, the error in arrow function overload is on the implementation signature. As a result, function can return If I use |
It can't be consistent with functions with current syntax. Function overloads has special syntax and therefore can provide special handling of return types. Your examples are regular function assignment and without special syntax are indistinguishable from incorrect implementations. type Overloaded = {
(a: number): number;
(a: string): string;
};
declare function correct(a: number | string): number & string;
declare function incorrect(a: number | string): number | string;
const f1: Overloaded = correct;
const f2: Overloaded = incorrect; // correctly errors You don't want to allow second case. To request similarity you should create proper feature request/proposal with convincing use cases, instead of using Bug report template. |
How is it possible that the "correct" function returns |
TypeScript Version: v3.5.1 (On TypeScript Playground)
Search Terms:
Arrow function overload
Code
Expected behavior:
The return value of implementation of overloading arrow function should be
number | string
which is allowed with function.Actual behavior:
TypeScript expect the return value of implementation of overloading arrow function should be
number & string
.Playground Link:
Link
Related Issues:
The text was updated successfully, but these errors were encountered: