Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
parloti committed Jan 21, 2024
2 parents 4028ef7 + 920f909 commit 445be6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
8 changes: 6 additions & 2 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ export default createRule<Options, MessageId>({
if (
isTypeFlagSet(
type,
ts.TypeFlags.Any | ts.TypeFlags.Unknown | ts.TypeFlags.TypeParameter,
ts.TypeFlags.Any |
ts.TypeFlags.Unknown |
ts.TypeFlags.TypeParameter |
ts.TypeFlags.TypeVariable,
)
) {
return;
Expand Down Expand Up @@ -345,7 +348,8 @@ export default createRule<Options, MessageId>({
flag |=
ts.TypeFlags.Any |
ts.TypeFlags.Unknown |
ts.TypeFlags.TypeParameter;
ts.TypeFlags.TypeParameter |
ts.TypeFlags.TypeVariable;

// Allow loose comparison to nullish values.
if (node.operator === '==' || node.operator === '!=') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ function test<T>(a: T) {
const t16 = undefined !== a;
}
`,
`
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] == null;
}
`,

// Predicate functions
`
Expand Down Expand Up @@ -317,6 +322,11 @@ function test<T>(a: T) {
`
function test<T extends string | null>(a: T) {
return a ?? 'default';
}
`,
`
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] ?? 'default';
}
`,
// Indexing cases
Expand Down Expand Up @@ -740,6 +750,11 @@ foo ||= 1;
`
declare let foo: number;
foo &&= 1;
`,
`
function foo<T extends object>(arg: T, key: keyof T): void {
arg[key] ??= 'default';
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/6264
`
Expand Down Expand Up @@ -1084,7 +1099,14 @@ function test(a: never) {
`,
errors: [ruleError(3, 10, 'never')],
},

{
code: `
function test<T extends { foo: number }, K extends 'foo'>(num: T[K]) {
num ?? 'default';
}
`,
errors: [ruleError(3, 3, 'neverNullish')],
},
// Predicate functions
{
code: `
Expand Down

0 comments on commit 445be6f

Please sign in to comment.