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

Properties in object literal key completion list aren't filtered when a prior argument supplies a mapped type key #35702

Closed
simonhaenisch opened this issue Dec 15, 2019 · 3 comments · Fixed by #36556
Assignees
Labels
Bug A bug in TypeScript Domain: Completion Lists The issue relates to showing completion lists in an editor

Comments

@simonhaenisch
Copy link

  • VSCode Version: 1.41.0
  • OS Version: macOS 10.14.6

Steps to Reproduce:

>> Playground Link

interface CustomElements {
    'component-one': {
        foo?: string;
    },
    'component-two': {
        bar?: string;
    }
}

interface Options<T extends keyof CustomElements> {
    props: CustomElements[T];
}

function create<T extends keyof CustomElements>(name: T, options: Options<T>) {
    return { name, options };
}

create('component-one', { props: { /* trigger intellisense here */ } });

const opts: Options<'component-one'> = {
    props: { /* and here */ }
}

The type of props in the create call is correctly narrowed down to CustomElements['component-one'] by the type system, however Intellisense suggests the props of all fields of CustomElements:

This does not happen when creating an object of that type:

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

@mjbvz mjbvz transferred this issue from microsoft/vscode Dec 16, 2019
@mjbvz mjbvz removed their assignment Dec 16, 2019
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Domain: Completion Lists The issue relates to showing completion lists in an editor labels Dec 19, 2019
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.8.1 milestone Dec 19, 2019
@RyanCavanaugh
Copy link
Member

@andrewbranch I seem to recall a duplicate of this, but searching is impossible

@RyanCavanaugh RyanCavanaugh changed the title Incorrect Typescript Intellisense with generics Properties in object literal key completion list aren't filtered when a prior argument supplies a mapped type key Dec 19, 2019
@andrewbranch
Copy link
Member

This was originally an unintended consequence of #32100/#33937, and while following up with #34855, spent a few days trying to come up with a way to get rid of these invalid entries without un-fixing #30507.

I talked to @DanielRosenwasser about weighing trade-offs, and @weswigham about other possible implementations, but I haven’t figured out a way to do this without speculatively checking possible future program states, which I think is prohibitively expensive. @DanielRosenwasser do you still feel the current behavior is an acceptable trade-off? I’m still not very happy with it, but not sure whether a much better solution is in sight.

@simonhaenisch
Copy link
Author

Ok that issue title makes more sense 🤓 Just want to add that the exact same thing also happens when the mapped type key is another field of the same argument (instead of a prior argument), i. e.

interface Options<T extends keyof CustomElements> {
    name: T;
    props: CustomElements[T];
}

create({ name: 'component-one', props: { /* trigger intellisense here */ } });

the current behavior is an acceptable trade-off?

Not sure why providing the programmer with invalid field names would be an acceptable trade-off?

Anyway, thanks for looking into this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment