Microsoft/TypeScript

Find All References does not report references in mapped types

Open

#57,115 opened on 2024幎1月21日

GitHub で芋る
 (0 comments) (0 reactions) (0 assignees)TypeScript (6,726 forks)batch import
Experience EnhancementHelp WantedSuggestion

Repository metrics

Stars
 (48,455 stars)
PR merge metrics
 (平均マヌゞ 6d 17h) (30d で 9 merged PRs)

説明

🔎 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

コントリビュヌタヌガむド