Typescript: Typescript Autocompletion Not Working For Function Overloads

Created on 5 Sep 2018  路  7Comments  路  Source: microsoft/TypeScript

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

Experience Enhancement Suggestion help wanted

All 7 comments

I'm also having an issue with autocomplete on overloaded code:

    public select<P extends NonReferenceNonFunctionPropNames<T>>(f: P): string;
    public select<P extends NonReferenceNonFunctionPropNames<T>>(f: FunctionField<T>): string;
    public select<P extends NonReferenceNonFunctionPropNames<T>>(f: P[]): string[];
    public select<P extends NonReferenceNonFunctionPropNames<T>>(f: P | P[] | FunctionField<T>): string | string[] {
        let relations = this._traversed.map(r => r.apiName);
        if (Array.isArray(f)) {
            let fieldArr: string[];
            fieldArr = f.map(field => {
                return this._obj.FIELDS[field as string].apiName
            });
            return fieldArr.map(field => {
                return [...relations, ...[field]].join('.');
            });
        } else if (typeof f === 'object') {
            let apiName = this._obj.FIELDS[f.field as string].apiName
            return renderComplexTypeText([...relations, ...[apiName]].join('.'), f.func, f.alias)
        } else {
            return [...relations, ...[this._obj.FIELDS[f as string].apiName]].join('.');
        }
    }

The auto-compete does work on the instances with P instances, but does NOT work when calling the P[] or FunctionField<T> (typechecking does still work, but it's very hard to remember these key names)

What is the expected behavior?
implement a partial signature resolve or fill parameters with correct type?

The signature help is wrong either if the first argument is Austria, the signature help still shows the first overload.

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.

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

I have a function with 100+ overloads.

Good luck....

I have a function with 100+ overloads.

Good luck....

@Kingwl
sad...
I write 100+ overloads in one go
and finally, i find that the autocomplete has been totally destroyed.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fdecampredon picture fdecampredon  路  358Comments

born2net picture born2net  路  150Comments

disshishkov picture disshishkov  路  224Comments

yortus picture yortus  路  157Comments

xealot picture xealot  路  150Comments