Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[non-nullable-type-assertion-style] complains when casting to a type parameter that might be nullish #4512

Closed
3 tasks done
djcsdy opened this issue Feb 4, 2022 · 0 comments · Fixed by #4509
Closed
3 tasks done
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@djcsdy
Copy link
Contributor

djcsdy commented Feb 4, 2022

  • 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-typer-assertion-style": "error"
  }
}
function first<T>(array: ArrayLike<T>): T | null {
    return array.length > 0 ? (array[0] as T) : null;
}

tsconfig:

  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "strict": true,
    "noUncheckedIndexedAccess": true
  },

Expected Result

typescript-eslint should not report any errors.

Actual Result

typescript-eslint complains about this type assertion: array[0] as T:

Use a ! assertion to more succintly remove null and undefined from the type @typescript-eslint/non-nullable-type-assertion-style

Additional Info

When compiling with noUncheckedIndexedAccess, the type assertion array[0] as T is required because the original type of array[0] is T | undefined.

However, non-nullable-type-assertion-style complains about the cast, suggesting that it should be array[0]! instead. This is not correct because the type parameter T might itself be nullish. array[0]! is equivalent to array[0] as NonNullable<T>, which is not the same as array[0] as T.

To see the issue more clearly, consider the following slightly more contrived example:

function first<T extends string | null>(array: ArrayLike<T>): T | null {
    return array.length > 0 ? (array[0] as T) : null;
}

non-nullable-type-assertion-style complains about this code too, but here the problem is more obvious. Clearly the type assertion array[0] as T is not equivalent to array[0]! because the type parameter T has a constraint that explicitly allows the type to be null.

See #4509 for a fix

Versions

package version
@typescript-eslint/eslint-plugin 5.10.2
@typescript-eslint/parser 5.10.2
TypeScript 4.5.5
ESLint 8.8.0
node 16.13.2
@djcsdy djcsdy added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Feb 4, 2022
@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working and removed triage Waiting for maintainers to take a look labels Feb 4, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 25, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue bug Something isn't working package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants