diff --git a/src/services/stringCompletions.ts b/src/services/stringCompletions.ts index abc27e438f41b..5c51f0c3b385e 100644 --- a/src/services/stringCompletions.ts +++ b/src/services/stringCompletions.ts @@ -130,7 +130,11 @@ namespace ts.Completions.StringCompletions { // bar: string; // } // let x: Foo["/*completion position*/"] - return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode((grandParent as IndexedAccessTypeNode).objectType)); + const { indexType, objectType } = grandParent as IndexedAccessTypeNode; + if (!rangeContainsPosition(indexType, position)) { + return undefined; + } + return stringLiteralCompletionsFromProperties(typeChecker.getTypeFromTypeNode(objectType)); case SyntaxKind.ImportType: return { kind: StringLiteralCompletionKind.Paths, paths: getStringLiteralCompletionsFromModuleNames(sourceFile, node, compilerOptions, host, typeChecker) }; case SyntaxKind.UnionType: { diff --git a/tests/cases/fourslash/completionListsStringLiteralTypeAsIndexedAccessTypeObject.ts b/tests/cases/fourslash/completionListsStringLiteralTypeAsIndexedAccessTypeObject.ts new file mode 100644 index 0000000000000..b504720b989db --- /dev/null +++ b/tests/cases/fourslash/completionListsStringLiteralTypeAsIndexedAccessTypeObject.ts @@ -0,0 +1,47 @@ +/// + +//// let firstCase: "a/*case_1*/"["foo"] +//// let secondCase: "b/*case_2*/"["bar"] +//// let thirdCase: "c/*case_3*/"["baz"] +//// let fourthCase: "en/*case_4*/"["qux"] +//// interface Foo { +//// bar: string; +//// qux: string; +//// } +//// let fifthCase: Foo["b/*case_5*/"] +//// let sixthCase: Foo["qu/*case_6*/"] + +// fourslash tests for issue 40322 +verify.completions({ + marker: ["case_1", "case_2", "case_3", "case_4"], + exact: undefined, + isNewIdentifierLocation: true, +}); + +// regression tests +const test5 = test.marker("case_5"); +const test6 = test.marker("case_6"); + +verify.completions({ + marker: "case_5", + includes: { + name: "bar", + replacementSpan: { + fileName: test5.fileName, + pos: test5.position - 1, + end: test5.position, + }, + }, +}); + +verify.completions({ + marker: "case_6", + includes: { + name: "qux", + replacementSpan: { + fileName: test6.fileName, + pos: test6.position - 2, + end: test6.position, + }, + }, +});