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
I am trying to achieve the equivalent of the above using function expressions instead of function declarations, however I get a type error:
/* Type '(x: string | number) => string | number' is not assignable to type '{ (x: string): string; (x: number): number; }'. Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string' */constidentity: {(x: string): string;(x: number): number;}=(x: string|number): string|number=>x;
How I can achieve the same effect of overloading the function but with function expressions?
Usage of function declarations vs expressions is a matter of preference. Ideally we would be able to achieve the same behaviour using either techniques.
Is this possible?
Is the above behaviour a bug?
The text was updated successfully, but these errors were encountered:
No. this is behaving as intended. there is a stronger relationship between overloads and an implementation signature that allows us to relax some of the rules.
Interesting, so it's not possible to achieve the same behaviour we see with the function declaration but using the function expression syntax? That's a shame. I guess we'll have to stick to function declarations for functions which require overloads.
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: 2.9.2
Search Terms:
Code function overload expression
Related #16731
I have this TypeScript code which uses overloads on a function declaration. This code works as expected.
I am trying to achieve the equivalent of the above using function expressions instead of function declarations, however I get a type error:
How I can achieve the same effect of overloading the function but with function expressions?
Usage of function declarations vs expressions is a matter of preference. Ideally we would be able to achieve the same behaviour using either techniques.
The text was updated successfully, but these errors were encountered: