SonarLint says cast is unnecessary

Please provide

  • Operating system: macOS Monterey 12.4
  • IDE name and flavor/env: PhpStorm
  • SonarLint plugin version:6.7.0.45926
  • Is connected mode used:No
    • Connected to SonarCloud or SonarQube (and which version):

And a thorough description of the problem / question:

import {isPositive} from './positive-number'

export type PositiveNumberRecord<T extends string | number | symbol> = Record<T, number> & { __type: 'positiveNumberRecord' }
export const isPositiveNumberRecord = <T extends string | number | symbol>(value: Record<T, number> | null | undefined): value is PositiveNumberRecord<T> => {
    if (!value)
        return false

    const values = Object.values(value) as number[]

    return values.every(isPositive)
}

SonarLint is telling me the “as number” is not necessary as it doesn’t change anything. That’s incorrect though, because typescript sees it as an unknown if I don’t do the type cast, and then I get a compiler error on the following line as isPositive expects a number, not an unknown.

Hello Gargoyle,

Welcome to the community and thanks for bringing this up!

  • Can you please confirm the name or number of the SonarLint rule you’re seeing?
  • What version of TypeScript are you using?

Thanks,
Gabriel

Hey,

I am not sure why exactly this FP happens, but I confirm I managed to reproduce (in VSCode) and created a ticket FP S4325 (`no-unnecessary-type-assertion`): `Object.values()` result · Issue #3235 · SonarSource/SonarJS · GitHub