typescript:S4325 false positive: confused between Partial<Type> and Type

Then tell us:

  • What language is this for?
    • Typescript
  • Which rule?
  • Why do you believe it’s a false-positive/false-negative?
    • When a function returns Partial and the returned value needs to be asserted to the full Type, typescript:S4325 reports that the value doesn’t need to be asserted, when it does.
  • Are you using
    • SonarQube Cloud?
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
function buildState(options: Options): Partial<State> {
  ...
}

function testState() {
  // Without the cast, state is Partial<State>. Whether or not that
  // makes state valid, SonarCloud shouldn't report that the assertion
  // doesn't change the type.
  const state = buildState(options) as State;
}

Hi @landisdesign,

Thanks for the report, and for taking the time to reduce the example and help improve the rule.

We tried to reproduce this with the reduced snippet on the latest analyzer, and also checked several older revisions, but we could not trigger typescript:S4325.

There has been recent work on our side to address false positives for this rule, but none of the changes we found clearly match this pattern. Since typescript:S4325 relies on the upstream @typescript-eslint/no-unnecessary-type-assertion rule, one possible explanation is that the behavior changed there, although we have not identified a specific change for this exact case.

If you can still reproduce it, could you share the exact analyzer version and a minimal reproducer that still raises the issue?

Thanks again for raising this.
François