Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-unnecessary-condition] fix false positive with computed member access and branded key type #7706

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 3 additions & 6 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,12 +534,9 @@ export default createRule<Options, MessageId>({
}
}
const typeName = getTypeName(checker, propertyType);
return !!(
(typeName === 'string' &&
checker.getIndexInfoOfType(objType, ts.IndexKind.String)) ||
(typeName === 'number' &&
checker.getIndexInfoOfType(objType, ts.IndexKind.Number))
);
return !!checker
.getIndexInfosOfType(objType)
.find(info => getTypeName(checker, info.keyType) === typeName);
yf-yang marked this conversation as resolved.
Show resolved Hide resolved
}

// Checks whether a member expression is nullable or not regardless of it's previous node.
Expand Down
143 changes: 143 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,149 @@ declare const key: Key;

foo?.[key]?.trim();
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/7700
`
type BrandedKey = string & { __brand: string };
type Foo = { [key: BrandedKey]: string } | null;
declare const foo: Foo;
const key = '1' as BrandedKey;
foo?.[key]?.trim();
`,
`
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'>;
declare const foo: Foo;
declare const key: Key;
foo?.[key].trim();
`,
`
type BrandedKey = string & { __brand: string };
interface Outer {
inner?: {
[key: BrandedKey]: string | undefined;
};
}
function Foo(outer: Outer, key: BrandedKey): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
`
interface Outer {
inner?: {
[key: string & { __brand: string }]: string | undefined;
bar: 'bar';
};
}
type Foo = 'foo' & { __brand: string };
function Foo(outer: Outer, key: Foo): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
`
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'> | BrandedKey<'baz'>;
declare const foo: Foo;
declare const key: Key;
yf-yang marked this conversation as resolved.
Show resolved Hide resolved
foo?.[key]?.trim();
`,
{
code: `
type BrandedKey = string & { __brand: string };
type Foo = { [key: BrandedKey]: string } | null;
declare const foo: Foo;
const key = '1' as BrandedKey;
foo?.[key]?.trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
yf-yang marked this conversation as resolved.
Show resolved Hide resolved
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'>;
declare const foo: Foo;
declare const key: Key;
foo?.[key].trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey = string & { __brand: string };
interface Outer {
inner?: {
[key: BrandedKey]: string | undefined;
};
}
function Foo(outer: Outer, key: BrandedKey): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
interface Outer {
inner?: {
[key: string & { __brand: string }]: string | undefined;
bar: 'bar';
};
}
type Foo = 'foo' & { __brand: string };
function Foo(outer: Outer, key: Foo): number | undefined {
return outer.inner?.[key]?.charCodeAt(0);
}
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
{
code: `
type BrandedKey<S extends string> = S & { __brand: string };
type Foo = { [key: string]: string; foo: 'foo'; bar: 'bar' } | null;
type Key = BrandedKey<'bar'> | BrandedKey<'foo'> | BrandedKey<'baz'>;
declare const foo: Foo;
declare const key: Key;
foo?.[key]?.trim();
`,
parserOptions: {
EXPERIMENTAL_useProjectService: false,
tsconfigRootDir: getFixturesRootDir(),
project: './tsconfig.noUncheckedIndexedAccess.json',
},
dependencyConstraints: {
typescript: '4.1',
},
},
`
let latencies: number[][] = [];

Expand Down