question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support overload resolution with type union arguments

See original GitHub issue

TypeScript 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:open
  • Created 7 years ago
  • Reactions:170
  • Comments:33 (10 by maintainers)

github_iconTop GitHub Comments

19reactions
DanielRosenwassercommented, Feb 16, 2017

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

interface Foo {
    bar(s1: string, s2: string): void;
    bar(n1: number, n2: number): number;
}

and you tried calling with the following:

declare var sn1: string | number;
declare var sn2: string | number;
declare var foo: Foo

foo(sn1, sn2);

Should our call to foo succeed? Probably not, since we can’t guarantee that sn1 and sn2 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

  1. It seems a little ad-hoc, and ideally we’d like to generalize the behavior.
  2. That process could be expensive (although this is less of a concern - this could be lazily done if overload resolution fails).
  3. This process is potentially not as trivial as it sounds at face value.
13reactions
burtekcommented, Feb 19, 2019

Just out of curiosity: what’s the status of this issue? 🤔

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found