microsoft / microsoft/TypeScript

Encapsulate type refactoring

Open
#31,573 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Search Terms

  • refactoring
  • encapsulate
  • return, function

Suggestion

Add a refactoring that encapsulates a given instance of a type and updates uses of it. Encapsulating would wrap the type in an object type that has a single property of that type.

Use cases

My original use case would be to convert a single returned type to an object that can return multiple values:

  1. Start
    function foo(): number { return Date.now(); }
    const z = foo(); 
    
  2. Select number and run encapsulate
  3. Result
    function foo(): { value: number } { return { value: Date.now() }; }
    const z = foo().value; 
    

This could also be used for individual parameters in a function:

  1. Start
    function foo(x: number) { return x * 2; }
    foo(1);
    
  2. Select number and run encapsulate
  3. Result
    function foo(x: { value: number }) { return x.value * 2; }
    foo({ value: 1 });
    

This may fall under some larger class of refactoring, I'm just not sure what to call it

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

The issue names no files or tests; use the foo return-type and parameter examples as the acceptance cases. First inspect the repository’s existing refactoring entry points and tests. Done means an Encapsulate action transforms both a selected return type and a function parameter, updating the shown call sites to use the resulting { value: ... } shape.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.