microsoft / microsoft/TypeScript

Find All References does not report references in mapped types

Open
#57,115 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Help Wanted Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms
  • find all references mapped type
🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about (n/a)

TypeScript Version 5.3.3

⏯ Playground Link

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgGQVSkwBs4BvAKDlrgGtkATALjgCJSi0z2BuGnQBuZAK7A2SUQFsARmgEBfKqEiw4MAJ5g8AUXBQ4AXgLcSpASvDR4WnXABqCAM5FoAWUxgdTY5UG0ANoA0syISHD6YFCB7IxITOwAuklsABSqUGz6MCTYMAA8UVAANJQMzGyhCXCKAHwAlMZ1cFJyClSKlqo2Gtp4Tq4w0AAiCFDA+X7UdHBcxGTpmWyEC6RNRi1t8lBKVFZq8NikmM7OcACSKGjRwMSe3sC+CNJgpMDSwCjng25QDz5-LMAPTAuCAXg3ACh7cAA4hANPCAErAdBoL64c4QCKcMy8DSYejAc6aCCiBEaAAWLjmyGAZSQEFs8K4SDwAA4AHQBGlrDIGFa49aSGQ7IGzOgTGCiKARTIckSkcQCWbKZQHXrHU7nK7EW7EMYTKYvN4fL4wH4uP4GybwGZ0UEQwD1O7D4cM4MjURMkBi4FiOPMeKR2HBnBSIAB3OAkskwKnnFl4TA1BNwACMACZuQHzHzogK1g1he1DHbxXBJdLZQZ5WJgMq6KqqEA

💻 Code
export interface Literal {
    kind: "literal";
    value: number;
}
export type Expr = Literal;

export type VisitorMapped = {
    [Kind in Expr["kind"]]: (expr: Extract<Expr, { kind: Kind }>) => number;
};

export type VisitorDirect = {
    literal: (expr: Literal) => number;
}

export class InterpreterMapped implements VisitorMapped {
    // 😔 Go to References on "literal" takes you to this line, not to line 8.
    literal(expr: Literal): number {
        return expr.value;
    }
}

export class InterpreterDirect implements VisitorDirect {
    // 💯 Go to References on "literal" show you this line and line 12
    literal(expr: Literal): number {
        return expr.value;
    }
}
🙁 Actual behavior

"Go to References" / "Find All References" has different behaviors for the two literals:

  • for literal in InterpreterDirect it shows two references (in VisitorDirect and InterpreterDirect)
  • for literal in InterpreterMapped it only shows the reference in InterpreterMapped, not in VisitorMapped.
🙂 Expected behavior

I'd expect "Go to References" / "Find All References" to report two references for both of the literals. The mapped type is a reference, even if it doesn't contain the identifier "literal" directly.

Additional information about the issue

This came up with Knip, which uses "find all references" to detect dead code. In this case this behavior caused a false positive: https://github.com/webpro/knip/issues/450

Possibly related to https://github.com/microsoft/TypeScript/issues/44643

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked Playground reproduction and compare Find All References for the mapped and direct visitor types. No source file or test is named, so trace the references implementation from that behavior. Done means both occurrences report the mapped-type member and the direct member consistently, with regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.