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.

Proposal: better type narrowing in overloaded functions through overload set pruning

See original GitHub issue

TypeScript Version: 2.7.2

Search Terms: type inference, type narrowing, overloads

Code

function bar(selector: "A", val: number): number;
function bar(selector: "B", val: string): string;
function bar(selector: "A" | "B", val: number | string): number | string
{
    if (selector === "A")
    {
        const the_selector = selector;  // "A"
        const the_val = val;            // number | string but should be just number
        return "foo";                   // should give error: when selector is "A" should return a number
    }
    else
    {
        const the_selector = selector;  // "B"
        const the_val = val;            // number | string but should be just string
        return 42;                      // should give error: when selector is "B" should return a string
    }
}

Issue

The snippet above shows a limitation of the current type inference implementation in TypeScript. As can be clearly seen from the overloaded declarations, when selector === "A" we have val: number and we must return a number, while when selector === "B" we have val: string and we must return a string. But the type-checker, although able to understand that the_selector can only be "A" in the if block and only "B" in the else block, still types the_val as number | string in both blocks and allows us to return a string when selector === "A" and a number when selector === "B".

Possible Solution

In the implementation of an overloaded function, when the type-checker decides to narrow the type of a parameter (as often happens in if-else blocks), it should also remove the overloads that don’t match the narrowed parameter type from the overload set, and use the pruned overload set to narrow the types of the other parameters.

Playground Link: https://www.typescriptlang.org/play/#src= function bar(selector%3A "A"%2C val%3A number)%3A number%3B function bar(selector%3A "B"%2C val%3A string)%3A string%3B function bar(selector%3A "A" | "B"%2C val%3A number | string)%3A number | string { if (selector %3D%3D%3D "A") { const the_selector %3D selector%3B const the_val %3D val%3B return "foo"%3B } else { const the_selector %3D selector%3B const the_val %3D val%3B return 42%3B } }

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:29
  • Comments:15

github_iconTop GitHub Comments

8reactions
Tiedyecommented, Aug 28, 2018

It would be fantastic if we could do something like this:

function f(a: number): void;
function f(a: string, b: string): void;
function f(a, b) {
  if (typeof a === 'number') {
    // `a` is a string and `b` is thus `undefined`
  }
}

The implementation would not be considered one of the overload signatures, and thus the arguments can be properly narrowed.

To preserve compatibility with implicit any, if the feature was only available when noImplicitAny is enabled it would be completely backwards compatible as it would only kick in when there were no type arguments on an overloaded constructor/method/function (right now this is an error).

3reactions
krryancommented, Mar 15, 2018

Absolutely agreed, this kind of thing would go a long way towards making overloaded functions a lot safer to use. But it might also be a breaking change: overloads can be used to assert/claim type relationships that TS cannot see. Enforcing particular rules may break attempts to use them to circumvent and/or augment those rules in the first place. But then, that usage of overloads always seemed dubious to me…

Also, related to #21879, which is requesting a similar thing for conditional types rather than overloads. The same problems raised there may also apply here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is it possible to narrow the types of overloaded parameters ...
TypeScript can narrow down the types of arguments if the narrowing is done with === . (A typeof check doesn't appear to work, ......
Read more >
Implementing Overloading and Polymorphism in Cforall - CiteSeerX
to a function with an overloaded name, it uses the number and types of the parameters to deter- mine the appropriate function to...
Read more >
C++ Standard Library Active Issues List
One possibility is overloading pbump, rather than changing the signature. ... because the narrowing happens inside the function body where no constant ...
Read more >
Groovy Language Documentation
SAM type coercion; Differences with Java 8 default methods ... Thanks to Groovy's operator overloading, the usual arithmetic operators work as well with...
Read more >
Chapter 15. Expressions
The value of an expression is assignment compatible (§5.2) with the type of the ... resolving a field name because of the possibility...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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