Navigation Menu

Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-condition] handle index signature…
Browse files Browse the repository at this point in the history
… type (#5912)

fix(eslint-plugin): [no-unnecessary-condition] handle mapped type
  • Loading branch information
yeonjuan committed Nov 15, 2022
1 parent 6a735e1 commit 5baad08
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Expand Up @@ -554,7 +554,12 @@ export default createRule<Options, MessageId>({
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 })
Expand Down
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
],
},
],
});

0 comments on commit 5baad08

Please sign in to comment.