Microsoft/TypeScript
View on GitHubFind All References does not report references in mapped types
Open
#57,115 opened on Jan 21, 2024
Experience EnhancementHelp WantedSuggestion
Repository metrics
- Stars
- (48,455 stars)
- PR merge metrics
- (Avg merge 6d 17h) (9 merged PRs in 30d)
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
💻 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
literalinInterpreterDirectit shows two references (inVisitorDirectandInterpreterDirect) - for
literalinInterpreterMappedit only shows the reference inInterpreterMapped, not inVisitorMapped.
🙂 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