diff --git a/packages/eslint-plugin/src/rules/dot-notation.ts b/packages/eslint-plugin/src/rules/dot-notation.ts index d0757f55a08..becc2a92727 100644 --- a/packages/eslint-plugin/src/rules/dot-notation.ts +++ b/packages/eslint-plugin/src/rules/dot-notation.ts @@ -110,10 +110,9 @@ export default createRule({ const objectType = typeChecker.getTypeAtLocation( esTreeNodeToTSNodeMap.get(node.object), ); - const indexType = typeChecker.getIndexTypeOfType( - objectType, - ts.IndexKind.String, - ); + const indexType = objectType + .getNonNullableType() + .getStringIndexType(); if (indexType != undefined) { return; } diff --git a/packages/eslint-plugin/tests/rules/dot-notation.test.ts b/packages/eslint-plugin/tests/rules/dot-notation.test.ts index cbdb2d52342..fe25e4de1f1 100644 --- a/packages/eslint-plugin/tests/rules/dot-notation.test.ts +++ b/packages/eslint-plugin/tests/rules/dot-notation.test.ts @@ -99,6 +99,25 @@ x['hello'] = 3; `, options: [{ allowIndexSignaturePropertyAccess: true }], }, + { + code: ` +interface Nested { + property: string; + [key: string]: number | string; +} + +class Dingus { + nested: Nested; +} + +let dingus: Dingus | undefined; + +dingus?.nested.property; +dingus?.nested['hello']; + `, + options: [{ allowIndexSignaturePropertyAccess: true }], + parserOptions: { ecmaVersion: 2020 }, + }, ], invalid: [ {