Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong typescript intelisense suggestions #31630

Open
Siegrift opened this issue May 24, 2019 · 2 comments
Open

Wrong typescript intelisense suggestions #31630

Siegrift opened this issue May 24, 2019 · 2 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@Siegrift
Copy link

  • VSCode Version: 1.34
  • OS Version: Linux Mint Tessa

The following code break the intelisense for TS.

export function get<T, K1 extends keyof T, K2 extends keyof T[K1]>(
  obj: T,
  keys: [K1, K2]
): T[K1][K2];
export function get<
  T,
  K1 extends keyof T,
  K2 extends keyof T[K1],
  K3 extends keyof T[K1][K2]
>(obj: T, keys: [K1, K2, K3]): T[K1][K2][K3];
export function get(obj: any, keys: any[]): any {
  for (const key of keys) obj = obj[key];
  return obj;
}

These are the TS suggestions
bad_intelsense1

However, they are wrong, because the third value needs to be property of the object on path 'a.b', precisely only value 'c'.

The typings are correct, because intelisense is able to infer the result
bad_intellisense2

Furthemore, it rejects all values except 'c', which means the intelisense should be able to provide correct suggestions.
bad_intelisense3

@mjbvz mjbvz transferred this issue from microsoft/vscode May 28, 2019
@mjbvz
Copy link
Contributor

mjbvz commented May 28, 2019

Tested with typescript@3.5.0-dev.20190525

Complete example:

export function get<T, K1 extends keyof T, K2 extends keyof T[K1]>(
    obj: T,
    keys: [K1, K2]
): T[K1][K2];
export function get<
    T,
    K1 extends keyof T,
    K2 extends keyof T[K1],
    K3 extends keyof T[K1][K2]
>(obj: T, keys: [K1, K2, K3]): T[K1][K2][K3];
export function get(obj: any, keys: any[]): any {
    for (const key of keys) obj = obj[key];
    return obj;
}

const obj = { a: { b: { c: 'x ' } } }
get(obj, ["a", 'b', ''])

@mjbvz mjbvz removed their assignment May 28, 2019
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 28, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone May 28, 2019
@tanhauhau
Copy link
Contributor

I'm interested to try on this one. Maybe some pointers where I should start looking into?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants