Skip to content

Commit b7741f8

Browse files
committed
fix(eslint-plugin): [no-unnecessary-condition] fix false positive for type variable
1 parent c3767ed commit b7741f8

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

Diff for: packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ export default createRule<Options, MessageId>({
271271
if (
272272
isTypeFlagSet(
273273
type,
274-
ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter,
274+
ts.TypeFlags.Any |
275+
ts.TypeFlags.Unknown |
276+
ts.TypeFlags.TypeParameter |
277+
ts.TypeFlags.TypeVariable,
275278
)
276279
) {
277280
return;
@@ -345,7 +348,8 @@ export default createRule<Options, MessageId>({
345348
flag |=
346349
ts.TypeFlags.Any |
347350
ts.TypeFlags.Unknown |
348-
ts.TypeFlags.TypeParameter;
351+
ts.TypeFlags.TypeParameter |
352+
ts.TypeFlags.TypeVariable;
349353

350354
// Allow loose comparison to nullish values.
351355
if (node.operator === '==' || node.operator === '!=') {

Diff for: packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ function test<T>(a: T) {
253253
const t16 = undefined !== a;
254254
}
255255
`,
256+
`
257+
function foo<T extends object>(arg: T, key: keyof T): void {
258+
const t1 = arg[key] == null;
259+
const t2 = null == arg[key];
260+
const t3 = arg[key] != null;
261+
const t4 = null != arg[key];
262+
const t5 = arg[key] == undefined;
263+
const t6 = undefined == arg[key];
264+
const t7 = arg[key] != undefined;
265+
const t8 = undefined != arg[key];
266+
const t9 = arg[key] === null;
267+
const t10 = null === arg[key];
268+
const t11 = arg[key] !== null;
269+
const t12 = null !== arg[key];
270+
const t13 = arg[key] === undefined;
271+
const t14 = undefined === arg[key];
272+
const t15 = arg[key] !== undefined;
273+
const t16 = undefined !== arg[key];
274+
}
275+
`,
256276

257277
// Predicate functions
258278
`
@@ -317,6 +337,11 @@ function test<T>(a: T) {
317337
`
318338
function test<T extends string | null>(a: T) {
319339
return a ?? 'default';
340+
}
341+
`,
342+
`
343+
function foo<T extends object>(arg: T, key: keyof T): void {
344+
arg[key] ?? 'default';
320345
}
321346
`,
322347
// Indexing cases
@@ -740,6 +765,13 @@ foo ||= 1;
740765
`
741766
declare let foo: number;
742767
foo &&= 1;
768+
`,
769+
`
770+
function foo<T extends object>(arg: T, key: keyof T): void {
771+
arg[key] ??= 'default';
772+
arg[key] ||= 'default';
773+
arg[key] &&= 'default';
774+
}
743775
`,
744776
// https://github.com/typescript-eslint/typescript-eslint/issues/6264
745777
`
@@ -1084,7 +1116,14 @@ function test(a: never) {
10841116
`,
10851117
errors: [ruleError(3, 10, 'never')],
10861118
},
1087-
1119+
{
1120+
code: `
1121+
function test<T extends { foo: number }, K extends 'foo'>(num: T[K]) {
1122+
num ?? 'default';
1123+
}
1124+
`,
1125+
errors: [ruleError(3, 3, 'neverNullish')],
1126+
},
10881127
// Predicate functions
10891128
{
10901129
code: `

0 commit comments

Comments
 (0)