microsoft / microsoft/TypeScript

Refactor option: Convert Object <-> Map

Open
#36,018 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

Search Terms

Refactor Maps to Objects
Refactor Objects to Maps
Convert objects convert maps

Suggestion

I would like to see the ability to right click on a map or object and be able to quickly and easily change the format between an object literal and a Map definition. Would help a lot when prototyping new code.

const o = {
    a: 1,
    b: 2,
};
const o = new Map([
    ["a", 1],
    ["b", 2],
]);

Use Cases

This is great when quickly prototyping new code and quickly typing the kind of object you want. If you decide a Map would be better suited for this use case after typing out an object, then you can easily just right click + refactor to isObject ? "Map" : "Object". At the moment, the easiest way to do this in VSCode that I'm aware of is to multi-cursor all the lines and try to modify the format on each line simultaneously, then edit the first and last lines to start with new Map([ and end with [)
It doesn't need to be SUPER sophisticated. The developer can easily change the Map type parameters, for example. It would just be nice to have a way to quickly convert between the two formats.

Examples

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the TypeScript editor refactoring implementation and its tests, then compare the requested object and Map examples. Done should provide a right-click conversion in both directions while preserving the represented entries and leaving type parameters editable.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.