Skip to content

Bug: [no-unnecessary-type-assertion] <Using type assertion on Object.values(Record<T> foo) gets wrongly flagged> #5808

@ilia-kebets-sonarsource

Description

Before You File a Bug Report Please Confirm You Have Done The Following...

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have searched for related issues and found none that matched my issue.
  • I have read the FAQ and my problem is not listed.

Playground Link

https://typescript-eslint.io/play/#ts=4.8.4&sourceType=module&code=KYDwDg9gTgLgBAMwK4DsDGMCWEV0wZwAUJ9MsA3YAChAC44UkBbAI2CgEo4BvAKDjhRgMJFFwg4APjgAGANy8Avr16hIsODACeYYHGKkKwAHLM2UAErA00ACYAeACpxQMYClv44+GFEwoAczgAHwYzdhDvLVYIABtpAF5+OCsbKAdHABow1nZpADIeOAB9Yu1degBySENMSlNcy2s7SrhFBVVwaHgbFB88IhIyOpNwprTbOAS4JxcQNw8vHz9AyMZGyPxoljjJZKpyAENYpGB6VLsnbPXzaVDGWNjI1FtgBH9gWw56I5O9An0QyMDXMF3STkS0j4yUwCDgVAAhL9Tlw+AIBEIRGJEMd8MAFAJlAJev1kcAvNMAPIsABW1hgADoyfgDscUXBDl4buwANoAXTkAgA9EK4AAxQgDOAAC3YwBUGOEolwzIZwEoUC0VAIBmGlA4CnaQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGYBfEWg&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYoEEkJemy0eAcgK6qoDCAFutAGsylBm3TgwAXxCSgA

Repro Code

export function isPositive(x: number) {
  return x > 0;
}

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[];  // FP is here

    return values.every(isPositive);
};

ESLint Config

module.exports = {
  parser: '@typescript-eslint/parser',
  parserOptions: {
    tsconfigRootDir: __dirname,
    project: ['./tsconfig.json']
  },
  plugins: ['@typescript-eslint'],
  rules: {},
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:@typescript-eslint/recommended-requiring-type-checking'
  ]
};

tsconfig

{
  "compilerOptions": {
      "module": "commonjs",
      "target": "es6",
      "lib": [
        "ES2017"
      ]
  },
  "include": [
      "src/index.ts"
  ]
}

Expected Result

I don't expect any error since the cast is necessary to change unknown[] to number[].

Actual Result

I'm getting an error:


  15:20  error  This assertion is unnecessary since it does not change the type of the expression  @typescript-eslint/no-unnecessary-type-assertion

✖ 1 problem (1 error, 0 warnings)
  1 error and 0 warnings potentially fixable with the `--fix` option.

Additional Info

No response

Versions

package version
@typescript-eslint/eslint-plugin 5.40.0
@typescript-eslint/parser 5.40.0
TypeScript 4.8.4
ESLint 8.25.0
node 16.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: eslint-pluginIssues related to @typescript-eslint/eslint-pluginworking as intendedIssues that are closed as they are working as intended

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions