Skip to content

Refactor to convert variable to destructuring #25946

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
ghost opened this issue Jul 25, 2018 · 4 comments
Closed

Refactor to convert variable to destructuring #25946

ghost opened this issue Jul 25, 2018 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Domain: Refactorings e.g. extract to constant or function, rename symbol Suggestion An idea for TypeScript

Comments

@ghost
Copy link

ghost commented Jul 25, 2018

TypeScript Version: 3.1.0-dev.20180721

Code

function f() { return { x: 0, y: 1, z: 2 }; }
const p = f();
p.x;
p.y;
p.z;

Expected behavior:

After:

function f() { return { x: 0, y: 1, z: 2 }; }
const { x, y, z } = f();
x;
y;
z;

Note: I think it should generate { x, y, z } even if they're not all currently used (meaning, it should use every property declared in the return type of f). That way it's useful to perform this refactor immediately after writing the call to f(), before the result has been used.

Actual behavior:

No such refactor.

@ghost ghost added Suggestion An idea for TypeScript Domain: Refactorings e.g. extract to constant or function, rename symbol labels Jul 25, 2018
@DanielRosenwasser
Copy link
Member

You probably want to be a little more careful here due to aliasing and assignment to p.x, p.y, or p.z.

@ghost
Copy link
Author

ghost commented Jul 25, 2018

If there's an assignment, we should probably not do the refactor -- let { x } = o; x = 2; does not mutate o as some might expect.

@RyanCavanaugh RyanCavanaugh added the Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature label Aug 22, 2018
@Kingwl Kingwl mentioned this issue Aug 19, 2020
14 tasks
@RyanCavanaugh
Copy link
Member

Declining due to low volume of feedback

@RyanCavanaugh RyanCavanaugh added Declined The issue was declined as something which matches the TypeScript vision and removed Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Dec 16, 2021
@OliverJAsh
Copy link
Contributor

The Abracadabra extension has tried to provide this refactoring but they've struggled to do it performantly—so I think there is definitely an appetite for this refactoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Domain: Refactorings e.g. extract to constant or function, rename symbol Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants