question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Key remapping breaks definition navigation for mapped type properties

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Feb 10, 2022

It definitely “should” work, but it’s a bit tricky to implement AFAIR

0reactions
thomasballingercommented, Sep 28, 2022

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!
Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Mapped Types - TypeScript
Key Remapping via as. In TypeScript 4.1 and onwards, you can re-map keys in mapped types with an as clause in a mapped...
Read more >
Typescript remap array to object with intellisense on keys
First the input object. If you use an explicit type, all information about specific values of the object you assign to it is...
Read more >
8 Creating and Using Mappings
A mapping is made up of and defined by the following parts: ... or enter the new attribute's name, data type, and other...
Read more >
What's New in EF Core 7.0 - Microsoft Learn
Mapping to JSON columns. In EF Core, aggregate types are defined using OwnsOne and OwnsMany . For example, consider the aggregate type from...
Read more >
Mapper XML Files - MyBatis.org
Both id and result map a single column value to a single property or field of a simple data type (String, int, double,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found