question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor: Introduce object destruction

See original GitHub issue

Search Terms

refactor, object destruction

Suggestion

A refactor that extract all property access expression to a object destruction or convert back

qq 20190215000331

Use Cases

Examples

const item = {
  a: 1, b: 2
}

call(item.a, item.b)

to

const item = {
  a: 1, b: 2
}

const { a, b } = item

call(a, b)

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.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:3
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
dragomirtitiancommented, Feb 18, 2019

@Kingwl While this does sound nice in theory I believe there would issues around unions. Type gurads work differently on union types so not everything that can be done with a union could be done with the distructured constituents. For example:

let u!: { type: 'number'; payload: number } | { type: 'string', payload: string }

if(u.type === "number") {
    u.payload.toExponential // payload is number
} else {
    u.payload.big // payload is string
}


let { type, payload } = u

if(type === "number") {
    payload.toExponential // error payload is string | number
} else {
    payload.big // error payload is string | number
}

This is not a reason not to implement this, but great care should be taken not to offer this refactoring when resulting code would break because of type guards.

1reaction
Kingwlcommented, Dec 22, 2019

I start working on this one :XD

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to refactor method of class with a lot of arguments?
Create an object to hold those 20 arguments and pass that object to the method. For example: public void MyMethod(MyArguments args) { //...
Read more >
31 Days of Refactoring - · Los Techies
This entails passing a reference to the class that will be returning the computation to a new object that has the multiple methods...
Read more >
Refactoring optional chaining into a large codebase - Lea Verou
When looking to use the optional chaining operator within Firefox DevTools, one thing we had to consider is how it affects project-wide search ......
Read more >
Terraform 1.3 Release Introduces Simplified Refactoring ...
The moved block was introduced in version 1.1 to provide a ... Moving or renaming an object informs Terraform to destroy the object...
Read more >
Refactoring Teddy Bear Destruction - Event Handling and Menus ...
Video created by Sistema de Universidades do ColoradoUniversidade do Colorado for the course "Intermediate Object-Oriented Programming for Unreal Games".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found