Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
4 tasks done
jdufresne opened this issue Mar 11, 2025 · 2 comments
Labels
duplicate This issue or pull request already exists enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jdufresne
Copy link

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

@jdufresne jdufresne added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 11, 2025
@Josh-Cena
Copy link
Member

I doubt anyone would think about Object.values as "type conversion". This sounds like it should be part of #6807.

@bradzacher
Copy link
Member

bradzacher commented Mar 12, 2025

Yaeh I'd agree -- this should be part of #6807 -- please propose it over there!

@bradzacher bradzacher added duplicate This issue or pull request already exists and removed triage Waiting for team members to take a look labels Mar 12, 2025
@github-actions github-actions bot added the locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. label Mar 20, 2025
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 20, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists enhancement: plugin rule option New rule option for an existing eslint-plugin rule locked due to age Please open a new issue if you'd like to say more. See https://typescript-eslint.io/contributing. package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

3 participants