Skip to content

Key remapping breaks definition navigation for mapped type properties #47813

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

Closed
ayatkevich opened this issue Feb 9, 2022 · 5 comments · Fixed by #51650
Closed

Key remapping breaks definition navigation for mapped type properties #47813

ayatkevich opened this issue Feb 9, 2022 · 5 comments · Fixed by #51650
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@ayatkevich
Copy link

Bug Report

When using mapped type with key remapping, definitions for the properties of mapped types are no longer discoverable. Though, references are still in there.

🔎 Search Terms

mapped types, key remapping, language server, editor hints

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mapped types

⏯ Playground Link

Playground link with relevant code

💻 Code

const object = {
    a: 1,    // ✅ can go to definition/references, can rename
    b: 2,    // ✅ can go to definition/references, can rename
    c: 'ok', // ✅ no references, can rename
};
/** ✅ rename of {@link object.a} and {@link object.b} will correctly rename all the references as well */

const remapByKey: {
    [P in keyof typeof object as P extends 'c' ? never : P]: 0;
} = { a: 0, b: 0 };

remapByKey.a; // ❌ can't go to definition, can't rename, ✅ yet highlights definition
remapByKey.b; // ❌ can't go to definition, can't rename, ✅ yet highlights definition
remapByKey.c; // ✅ errors as expected

const remapByValue: {
    [P in keyof typeof object as typeof object[P] extends string ? never : P]: 0;
} = { a: 0, b: 0 };

remapByValue.a; // ❌ can't go to definition, can't rename, ✅ yet highlights definition
remapByValue.b; // ❌ can't go to definition, can't rename, ✅ yet highlights definition
remapByValue.c; // ✅ errors as expected

🙁 Actual behavior

image

🙂 Expected behavior

Capability to navigate to definition of remapped property.

@RyanCavanaugh
Copy link
Member

The highlighting you see is purely syntactic; unrelated instances of a will also highlight for example.

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Feb 9, 2022
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Feb 9, 2022
@ayatkevich
Copy link
Author

@RyanCavanaugh ah, I see.

What about the capability to go to definition? In remapped type you can go to reference from any reference or definition point. But you can't go to definition from a reference.
Contrary, in a mapped type without remapping, you can go back and forth between definition and reference, in both directions.

@RyanCavanaugh
Copy link
Member

It definitely "should" work, but it's a bit tricky to implement AFAIR

@sfedorov-at-wiley
Copy link

sfedorov-at-wiley commented Apr 2, 2022

Still does not work
But it definitely will be nice to make a hint for IDE, that mapped key refers to corresponding key in given object
image

@thomasballinger
Copy link

I encountered a case where this nearly works, which makes me wonder if there's a worthwhile partial fix.

When I use key remapping on a record of functions, the functions can be found once I add parens. "Go to type definition" in VSCode works with and without parens.

playground link

const funcs = { foo: () => {}, bar: () => {} }

const c = {} as { [K in keyof typeof funcs as K]: typeof funcs[K] }
c.foo   // doesn't cmd-click (known, that's this issue #47813) but "go to type definition" works (VSCode)
c.foo() // cmd-click works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants