Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Rule proposal: PreventObject.values(...) usage withMap #6807

Open
Labels
accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin
@MadLittleMods

Description

@MadLittleMods

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

My proposal is suitable for this project

  • My proposal specifically checks TypeScript syntax, or it proposes a check that requires type information to be accurate.
  • My proposal is not a "formatting rule"; meaning it does not just enforce how code is formatted (whitespace, brace placement, etc).
  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Description

UsingObject.values(...),Object.keys(...),Object.entries(...) with aMap object is a foot-gun because it will return empty[] regardless of the map contents and is most likely a mistake. These sorts of mistakes are easy to get into during refactors. This issue is spawning from a real-life refactor mistake ->element-hq/element-web#24995 (comment)

Related StackOverflow question:https://stackoverflow.com/questions/72315392/how-to-prevent-the-mistake-of-calling-object-values-on-a-map

As@bergus mentions from the SO question, it's possible to extendMap to make a subclass that returns something with the object utilities but this seems a lot more niche compared to the big problem of running them on baseMap objects. AFAICT, it's not possible to use TypeScript on its own to catch this sort of thing because practically everything inherits fromObject and function arguments in TypeScript are contravariant (accepts supertypes but doesn't accept subtypes).

Fail Cases

constmap=newMap([[1,'one'],[2,'two']]);// Rule would error for each of these usages, as using these object utilities// on a Map will just return empty results and is probably a mistakeObject.keys(map);// -> []Object.values(map);// -> []Object.entries(map);// -> []

Pass Cases

constmyObject={1:'one',2:'two'};Object.keys(myObject);// -> ['1', '2']Object.values(myObject);// -> ['one', 'two']Object.entries(myObject);// -> [[ 1, 'one' ],[ 2, 'two' ]]constmap=newMap([[1,'one'],[2,'two']]);map.keys();// -> ['1', '2']map.values();// -> ['one', 'two']map.entries();// -> [[ 1, 'one' ],[ 2, 'two' ]]

Additional Info

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: new plugin ruleNew rule request for eslint-pluginpackage: 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


      [8]ページ先頭

      ©2009-2025 Movatter.jp