Find All References does not report references in mapped types
#57,115 opened on 2024幎1æ21æ¥
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
ð» 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