"Assertion is unnecessary" identified incorrectly

Please provide

  • Operating system: Win10
  • SonarLint plugin version: v3.15.1
  • Programming language you’re coding in: Typescript
  • Is connected mode used:
    • Connected to SonarCloud or SonarQube (and which version): no

And a thorough description of the problem / question:

Hello, Team!

I noticed that in my project, SonarLint incorrectly identifies an assertion as unnecessary, but it is necessary indeed (see attached file). Can you please fix this?
AssertionUnnecessaryIncorrect|690x388
SonarLog.log (7.2 KB)

Hi @Omcsesz, Welcome, and thank you for your feedback.

First, I would like to confirm with you the use-case details. I can reproduce the same behavior by assuming that the signature of jwtDecode() is:

function jwtDecode<T>(token: string): T {}

In this case, the issue appears if we use an as type assertion to specify the function return type. But we can avoid the problem by adding a type annotation to the variable instead.

Does the signature of jwtDecode() correspond to your case?
Does the type annotation solution work for you?

Regards
Francois

The signature for jwtDecode() is:

export default function jwtDecode<T = unknown>(
  token: string,
  options?: JwtDecodeOptions
): T;

The type annotation solution works for me.
Can you please fix this in the next release?

Thanks for the suggestion @Omcsesz
Before working on this rule, we will wait for more feedback to determine which option is preferable between type annotations and type assertions for functions with a generic return type.
Regards
François

Why can’t have both?