Skip to content

Enhancement: [no-unnecessary-type-conversion] Treat Object.values(array) as an unnecessary type conversion #10941

Closed as duplicate
@jdufresne

Description

@jdufresne

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

#10182

Description

As an enhancement of the no-unnecessary-type-conversion rule proposed in #8515 and implemented in #10182, I propose that Object.values(array) be treated as an unnecessary type conversion as the expression can be simplified to just array.

Examples:

// fail
Object.values(['a', 'b', 'c'])  // simplify to: ['a', 'b', 'c']

const ar = ['a', 'b', 'c']
Object.values(ar)  // simplify to: ar


// pass
type MyObject = { a: string }

let arrayOrObject: array | MyObject
arrayOrObject = ['a', 'b', 'c']
Object.values(arrayOrObject)  // could be an object, can't simplify

I have discovered Object.values(array) within my own projects, it is possible this unnecessary type coercion exists elsewhere too.

Fail

Object.values(['a', 'b', 'c'])

const ar = ['a', 'b', 'c']
Object.values(ar)  // simplify to: ar

Pass

['a', 'b', 'c']

const ar = ['a', 'b', 'c']
Object.values(ar)

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already existsenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulelocked due to agePlease open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing.package: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions