Skip to content

[non-nullable-type-assertion-style] wrong autofix for function calls with generic return value #3938

@FloEdelmann

Description

@FloEdelmann
  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "@typescript-eslint/non-nullable-type-assertion-style": "error"
  }
}
import rewire from 'rewire'
const rewiredModule = rewire('./example-module')
const someFunction = rewiredModule.__get__('myFunc') as (() => void)

Expected Result

This rule warns about the rewiredModule.__get__('myFunc') as (() => void) part. According to #2887 (comment), this is expected. As __get__ is a function with a generic type only used in the return type (see here), I'd expect no autofix in this case, or this one:

  import rewire from 'rewire'
  const rewiredModule = rewire('./example-module')
- const someFunction = rewiredModule.__get__('myFunc') as (() => void)
+ const someFunction = rewiredModule.__get__<() => void>('myFunc')

Actual Result

The autofix applies this (wrong) change:

  import rewire from 'rewire'
  const rewiredModule = rewire('./example-module')
- const someFunction = rewiredModule.__get__('myFunc') as (() => void)
+ const someFunction = rewiredModule.__get__('myFunc')!

Additional Info

The rewire module's source code: https://github.com/jhnns/rewire

As already suggested, the autofix could just become a suggestion instead.

Versions

package version
@typescript-eslint/eslint-plugin 4.31.1
@typescript-eslint/parser 4.31.1
TypeScript 4.3.4
ESLint 7.32.0
node 12.22.6

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomerspackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions