Support overload resolution with type union arguments
See original GitHub issueTypeScript Version: 2.1.6
Code
interface Foo {
bar(s: string): void;
bar(n: number): number;
bar(b: boolean): boolean;
}
type SN = string | number;
var sn1: string | number;
var sn2: SN;
var foo: Foo;
var x1 = foo.bar(sn1); // error
var x2 = foo.bar(sn2); // error
Expected behavior:
This should be allowed.
The type of x1
and x2
should be void | number
, the union of the matching overload return types.
All 3 overloads can be seen as a single overload with a union type. It should try to fallback to a union when it can’t match one of the originally defined overloads.
Actual behavior: error TS2345: Argument of type ‘string | number’ is not assignable to parameter of type ‘boolean’. Type ‘string’ is not assignable to type ‘boolean’.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:170
- Comments:33 (10 by maintainers)
Top Results From Across the Web
Overload resolution - cppreference.com
The set of candidate functions to be submitted for overload resolution is a union of the sets above. The argument list for the...
Read more >Overload signatures, union types and "No overload matches ...
Passing Unions to An Overload. Typescript is not able to "split up" the union before checking it against your overload signatures.
Read more >Generics vs Function Overloading vs Union Type Arguments ...
Generic Functions are the only solution you have to solve the common problem between union types and function overloading — the capacity of ......
Read more >Handbook - Unions and Intersection Types - TypeScript
Intersection and Union types are one of the ways in which you can compose types. Union Types. Occasionally, you'll run into a library...
Read more >Changes in Overload Resolution - Scala 3 - EPFL
In a situation where a function is applied to more than one argument list, if overloading resolution yields several competing alternatives when n...
Read more >Top Related Medium Post
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I can’t find other issues where this discussion’s been had, but I’ll try to give some context. The single-parameter case always tends to be the most frustrating one for people, and the most obvious one to fix. The problem occurs when you have multiple overloads.
For example, suppose you had the following overloads
and you tried calling with the following:
Should our call to
foo
succeed? Probably not, since we can’t guarantee thatsn1
andsn2
share the same type.So we could come up with a way of trying to collapse overloads that differ by exactly one parameter into unions and retying the overload process, but I think our concerns are
Just out of curiosity: what’s the status of this issue? 🤔