Typescript Autocompletion Not Working For Function Overloads
See original GitHub issueFrom @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).
- Create a function with two overloads
- Try to invoke this function
- 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!
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:
- Created 5 years ago
- Reactions:2
- Comments:8 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
any updates on this? I have a function with 100+ overloads. needs this feature very much!
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:
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:
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:
In this playground you will find both approaches. Just try the autocompletion for the calls of both
a
andb
.