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.

Typescript Autocompletion Not Working For Function Overloads

See original GitHub issue

From @MartinLoeper on August 29, 2018 13:27

  • VSCode Version: 1.25.1 1dfc5e557209371715f655691b1235b6b26a06be x64
  • OS Version: Linux version 4.15.0-32-generic Ubuntu

Steps to Reproduce:
I provided a small example of the issue (see screenshot below).

  1. Create a function with two overloads
  2. Try to invoke this function
  3. Once you typed in the first parameter, the IDE recognizes that it matches the first overload’s signature. However, the auto-completion suggest the literals for both overloads.

I expect the auto-completion to show “BW” and “BY” only!

_005

Does this issue occur when all extensions are disabled?: Yes

The code:


export class MomentHolidayFactory {
    public a(input: Germany, second: GermanState): string;
    public a(input: Austria, second: AustrianState): string;

    public a(input: Country, second: State): string {
        return "test";
    }

    public holidays() {
        this.a("de", ""
    }
}

type Country = Germany & Austria;
type Germany = "de" | "De" | "DE" | "germany";
type Austria = "Au" | "au" | "AU" | "austria";

type State = GermanState & AustrianState;
type GermanState = "BW" | "BY";
type AustrianState = "Stmk" | "Vbg";

Copied from original issue: Microsoft/vscode#57509

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
jueinincommented, Nov 18, 2020

any updates on this? I have a function with 100+ overloads. needs this feature very much!

1reaction
aloifoliacommented, Jul 6, 2020

I recently encountered the same problem where I wanted the compiler to restrict arguments based on previous arguments for an overloaded function type. Also, I came up with a solution that works quite well, in my opinion.

First of all, your types need a little fix:

// intersection types do not make much sense, you actually want union types
type Country = Germany | Austria;
type State = GermanState | AustrianState;

With this fix in mind, you can change the function signature in such a manner that you will basically be able to have partial application of your function:

// I removed the class "wrapper" to keep the example small - this should work for methods, as well.
function b(input: Germany): (second: GermanState) => string;
function b(input: Austria): (second: AustrianState) => string;
function b(input: Country): (second: State) => string {
    return second => "test";
}

Now, you are forced to apply the function b twice. For the second application, VSCode will suggest more fitting values because the inference of the compiler is “locked” into the return type of the first application:

Screenshot 2020-07-06 at 12 59 18

In this playground you will find both approaches. Just try the autocompletion for the calls of both a and b.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript overload functions don't autocomplete correctly in IDE
Great question. it seems Typescript is unable to infer the narrowest type (from the first parameter) when working with overloads.
Read more >
A Simple Explanation of Function Overloading in TypeScript
Function overloading in TypeScript lets you define functions that can be called in multiple ways.
Read more >
Intellisense and methods overload not working properly
Intellisense and method overload list are only working by pressing ctrl + space. Visual Studiowindows 10.0intellisenseVisual Studio 2017 version 15.2.
Read more >
How to document overloaded functions? : r/typescript - Reddit
That is, define your function type separately as a type or interface and intellisense will work for every iteration.
Read more >
TypeScript: JavaScript With Syntax For Types.
Describe the shape of objects and functions in your code. Making it possible to see documentation and issues in your editor. ts. interface ......
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