Skip to content

Commit 1d2cacd

Browse files
committed
fix(56052): allow rename string literal props
1 parent 9473195 commit 1d2cacd

5 files changed

+48
-1
lines changed

src/services/findAllReferences.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,7 @@ export namespace Core {
24462446
if (isStringLiteralLike(ref) && ref.text === node.text) {
24472447
if (type) {
24482448
const refType = getContextualTypeFromParentOrAncestorTypeNode(ref, checker);
2449-
if (type !== checker.getStringType() && type === refType) {
2449+
if (type !== checker.getStringType() && (type === refType || isStringLiteralPropertyReference(ref, checker))) {
24502450
return nodeEntry(ref, EntryKind.StringLiteral);
24512451
}
24522452
}
@@ -2464,6 +2464,13 @@ export namespace Core {
24642464
}];
24652465
}
24662466

2467+
function isStringLiteralPropertyReference(node: StringLiteralLike, checker: TypeChecker) {
2468+
const container = findAncestor(node, or(isInterfaceDeclaration, isTypeLiteralNode));
2469+
if (container) {
2470+
return checker.getPropertyOfType(checker.getTypeAtLocation(container), node.text);
2471+
}
2472+
}
2473+
24672474
// For certain symbol kinds, we need to include other symbols in the search set.
24682475
// This is not needed when searching for re-exports.
24692476
function populateSearchSymbolSet(symbol: Symbol, location: Node, checker: TypeChecker, isForRename: boolean, providePrefixAndSuffixText: boolean, implementations: boolean): Symbol[] {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// === findRenameLocations ===
2+
// === /tests/cases/fourslash/renameStringLiteralTypes4.ts ===
3+
// interface I {
4+
// <|"[|Prop 1RENAME|]": string;|>
5+
// }
6+
//
7+
// declare const fn: <K extends keyof I>(p: K) => void
8+
//
9+
// fn("[|Prop 1RENAME|]"/*RENAME*/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// === findRenameLocations ===
2+
// === /tests/cases/fourslash/renameStringLiteralTypes5.ts ===
3+
// type T = {
4+
// <|"[|Prop 1RENAME|]": string;|>
5+
// }
6+
//
7+
// declare const fn: <K extends keyof T>(p: K) => void
8+
//
9+
// fn("[|Prop 1RENAME|]"/*RENAME*/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////interface I {
4+
//// "Prop 1": string;
5+
////}
6+
////
7+
////declare const fn: <K extends keyof I>(p: K) => void
8+
////
9+
////fn("Prop 1"/**/)
10+
11+
verify.baselineRename("", {});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////type T = {
4+
//// "Prop 1": string;
5+
////}
6+
////
7+
////declare const fn: <K extends keyof T>(p: K) => void
8+
////
9+
////fn("Prop 1"/**/)
10+
11+
verify.baselineRename("", {});

0 commit comments

Comments
 (0)