Skip to content

Commit 64a8867

Browse files
authored
Fix spelling codefix for optional chain property access (#35583)
1 parent 9a9baeb commit 64a8867

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/services/codefixes/fixSpelling.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ namespace ts.codefix {
3737
let suggestion: string | undefined;
3838
if (isPropertyAccessExpression(node.parent) && node.parent.name === node) {
3939
Debug.assert(node.kind === SyntaxKind.Identifier, "Expected an identifier for spelling (property access)");
40-
const containingType = checker.getTypeAtLocation(node.parent.expression);
40+
let containingType = checker.getTypeAtLocation(node.parent.expression);
41+
if (node.parent.flags & NodeFlags.OptionalChain) {
42+
containingType = checker.getNonNullableType(containingType);
43+
}
4144
suggestion = checker.getSuggestionForNonexistentProperty(node as Identifier, containingType);
4245
}
4346
else if (isImportSpecifier(node.parent) && node.parent.name === node) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @strict: true
4+
5+
////function f(x: string | null) {
6+
//// [|x?.toLowrCase();|]
7+
////}
8+
9+
verify.rangeAfterCodeFix(
10+
`x?.toLowerCase();`,
11+
/*includeWhiteSpace*/ false,
12+
/*errorCode*/ undefined,
13+
/*index*/ 0);

0 commit comments

Comments
 (0)