Skip to content
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

Refactor: Introduce object destruction #29917

Open
5 tasks done
Kingwl opened this issue Feb 14, 2019 · 9 comments
Open
5 tasks done

Refactor: Introduce object destruction #29917

Kingwl opened this issue Feb 14, 2019 · 9 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@Kingwl
Copy link
Contributor

Kingwl commented Feb 14, 2019

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.
@RyanCavanaugh
Copy link
Member

This doesn't deserve an example of what it would do?

@Kingwl
Copy link
Contributor Author

Kingwl commented Feb 18, 2019

Updated

@dragomirtitian
Copy link
Contributor

@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.

@Kingwl
Copy link
Contributor Author

Kingwl commented Apr 19, 2019

I‘m very interested in this one if we accept the pr

@whimzyLive
Copy link

any updates on this feature?

@RyanCavanaugh
Copy link
Member

@wimzyLive no

@Kingwl
Copy link
Contributor Author

Kingwl commented Dec 22, 2019

I start working on this one :XD

@Kingwl Kingwl mentioned this issue Jul 30, 2020
14 tasks
@bigaru
Copy link
Contributor

bigaru commented Aug 19, 2020

It seems to be a duplicate of #25946

@Kingwl
Copy link
Contributor Author

Kingwl commented Aug 19, 2020

@bigaru
Indeed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants