Skip to content

Bug: non-nullable-type-assertion-style false positive for generic functions #954

@atitcreate

Description

@atitcreate

cd to a temp dir, run:

npm install --save-dev eslint typescript typescript-eslint \
    oxlint oxlint-tsgolint
npx tsc --init

create eslint.config.mjs:

import { defineConfig } from "eslint/config"
import tseslint from "typescript-eslint"
export default defineConfig({
  files: ["test.ts"],
  extends: [tseslint.configs.base],
  rules: {
    "@typescript-eslint/non-nullable-type-assertion-style": "error",
  },
  languageOptions: {
    parserOptions: {
      projectService: true,
    },
  },
})

create .oxlintrc.json:

{
  "options": { "typeAware": true },
  "categories": { "correctness": "off" },
  "rules": {
    "typescript/non-nullable-type-assertion-style": "error"
  }
}

create test.ts:

const href = (document.querySelector(".anchor") as HTMLAnchorElement).href
npx eslint ; npx oxlint
# Both prompt "Use a ! assertion to more succinctly remove null and undefined from the type."

Both npx eslint --fix and npx oxlint --fix apply the same fix:

-const href = (document.querySelector(".anchor") as HTMLAnchorElement).href
+const href = (document.querySelector(".anchor")!).href

The issue is in typescript-eslint repo labelled "external" "wontfix" "working as intended"

typescript-eslint/typescript-eslint#528 (comment)
typescript-eslint/typescript-eslint#9007 (comment)

Maybe at least for oxlint the auto-fix can be marked as "dangerous fix"?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions