diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 51d42f82926..030ed4fe188 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -554,7 +554,12 @@ export default createRule({ type, property.name, ); - return propType && isNullableType(propType, { allowUndefined: true }); + + if (propType) { + return isNullableType(propType, { allowUndefined: true }); + } + + return !!checker.getIndexInfoOfType(type, ts.IndexKind.String); }); return ( !isOwnNullable && isNullableType(prevType, { allowUndefined: true }) diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index f8401399a20..af8b189f899 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -527,6 +527,24 @@ if (x) { tsconfigRootDir: path.join(rootPath, 'unstrict'), }, }, + ` +interface Foo { + [key: string]: [string] | undefined; +} + +type OptionalFoo = Foo | undefined; +declare const foo: OptionalFoo; +foo?.test?.length; + `, + ` +interface Foo { + [key: number]: [string] | undefined; +} + +type OptionalFoo = Foo | undefined; +declare const foo: OptionalFoo; +foo?.[1]?.length; + `, ], invalid: [ // Ensure that it's checking in all the right places @@ -1548,5 +1566,36 @@ if (x) { tsconfigRootDir: path.join(rootPath, 'unstrict'), }, }, + { + code: ` +interface Foo { + test: string; + [key: string]: [string] | undefined; +} + +type OptionalFoo = Foo | undefined; +declare const foo: OptionalFoo; +foo?.test?.length; + `, + output: ` +interface Foo { + test: string; + [key: string]: [string] | undefined; +} + +type OptionalFoo = Foo | undefined; +declare const foo: OptionalFoo; +foo?.test.length; + `, + errors: [ + { + messageId: 'neverOptionalChain', + line: 9, + endLine: 9, + column: 10, + endColumn: 12, + }, + ], + }, ], });