Skip to content

linter: no-unnecessary-condition false positive on Partial<Record> index access with noUncheckedIndexedAccess #20149

@DreierF

Description

@DreierF

What version of Oxlint are you using?

1.51.0, oxlint-tsgolint 0.16.0

What command did you run?

oxlint

What does your .oxlintrc.json (or oxlint.config.ts) config file look like?

  {
    "plugins": ["typescript"],
    "options": {
      "typeAware": true
    },
    "rules": {
      "@typescript-eslint/no-unnecessary-condition": "error"
    }
  }

What happened?

@typescript-eslint/no-unnecessary-condition reports a false positive when indexing into a Partial<Record<R, T[]>> and using ?? [] as a fallback. With noUncheckedIndexedAccess: true in tsconfig.json, the index access type is T[] | undefined, so the nullish coalescing is necessary. oxlint incorrectly considers the value non-nullish.

tsconfig.json:

  {
    "compilerOptions": {
      "module": "ESNext",
      "target": "ES2023",
      "moduleResolution": "bundler",
      "strict": true,
      "noEmit": true,
      "noUncheckedIndexedAccess": true,
      "skipLibCheck": true
    },
    "include": ["src"]
  }

src/example.ts:

export function groupBy<T, R extends string | number>(list: T[], func: (item: T) => R) {
    return list.reduce(
      (obj, item) => {
        const existingItems = obj[func(item)] ?? [];
        return { ...obj, [func(item)]: [...existingItems, item] };
      },
      {} as Partial<Record<R, T[]>>
    );
  }

Reported diagnostic:

    x typescript-eslint(no-unnecessary-condition): Unnecessary optional chain on a non-nullish value.
     ,-[src/example.ts:4:26]
   3 |         (obj, item) => {
   4 |             const existingItems = obj[func(item)] ?? [];
     :                                   ^^^^^^^^^^^^^^^
   5 |             return { ...obj, [func(item)]: [...existingItems, item] };
     `----

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions