Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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