Skip to content

Inaccurate Intellisense / Suggestions with Function Overloads #51047

Open
@no-stack-dub-sack

Description

@no-stack-dub-sack

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

  • VS Code Version:
    Version: 1.69.1 (system setup)
    Commit: b06ae3b2d2dbfe28bca3134cc6be65935cdfea6a
    Date: 2022-07-12T08:21:24.514Z
    Electron: 18.3.5
    Chromium: 100.0.4896.160
    Node.js: 16.13.2
    V8: 10.0.139.17-electron.0
    OS: Windows_NT x64 10.0.19044

Steps to Reproduce:

  1. Go to this playground and/or create a new TypeScript file with the code snippet shared below
  2. After "c" on the last line, type , "i"
  3. Note the intellisense and suggestions

Apologies, there's probably a better title for this issue...

I thought this might be a better issue for Monaco, but their issue template said that if I could recreate the issue in VS Code to open the issue here. Maybe this should be a TS issue instead since its reproducible in the playground as well? However, the TypeScript seems to be working fine, it's just the intellisense that seems to be confused.

Take the following code:

function doSomething<Type extends object, Key extends keyof Type>(
  item: Type,
  ...keys: Key[]
): void;

function doSomething<Type extends object, Key extends keyof Type>(
  items: Type[],
  ...keys: Key[]
): void;

function doSomething<Type extends object, Key extends keyof Type>(
  itemOrItems: Type,
  ...keys: Key[]
) {
  // omitted for brevity
}

type Foo = {
  a: number;
  b: number;
  c: string;
};

const x = doSomething({ a: 1, b: 2, c: "" } as Foo, "a", "b", "c");
const y = doSomething([{ a: 1, b: 2, c: "" }] as Foo[], "a", "c");

When spreading arguments into the function corresponding with the second overload, the intellisense provides inaccurate suggestions and incorrectly infers ...keys as (keyof Foo[])[] when it should actually be (keyof Foo)[]:

image

Again, note that TypeScript is working as expected, and accepting any of the suggestions provided by the intellisense will result in a type error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions