Skip to content

Refactor: Introduce object destruction #29917

Open

Description

@Kingwl

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.

Activity

  1. RyanCavanaugh commented on Feb 15, 2019

    @RyanCavanaugh
    Member

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

  2. Kingwl commented on Feb 18, 2019

    @Kingwl
    ContributorAuthor

    Updated

  3. dragomirtitian commented on Feb 18, 2019

    @dragomirtitian
    Contributor

    Wenlu Wang (@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.

  4. Kingwl commented on Apr 19, 2019

    @Kingwl
    ContributorAuthor

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

  5. whimzyLive commented on Dec 19, 2019

    @whimzyLive

    any updates on this feature?

  6. RyanCavanaugh commented on Dec 20, 2019

    @RyanCavanaugh
    Member

    @wimzyLive no

  7. Kingwl commented on Dec 22, 2019

    @Kingwl
    ContributorAuthor

    I start working on this one :XD

  8. bigaru commented on Aug 19, 2020

    @bigaru
    Contributor

    It seems to be a duplicate of #25946

  9. Kingwl commented on Aug 19, 2020

    @Kingwl
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions