Skip to content

Combining destructuring with parameter properties #5326

Open
@buzinas

Description

@buzinas

Today, we can take advantage of parameter properties to reduce the boilerplate, e.g:

class Person {
  constructor(public firstName: string, public lastName: number, public age: number) {

  }
}

Since 1.5, we can also use destructuring, e.g:

class Person {
  firstName: string;
  lastName: string;
  age: number;

  constructor({ firstName, lastName, age } : { firstName: string, lastName: string, age: number }) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
  }
}

I've tried in many ways to combine both features, but had no success. So:

  1. Is it possible to combine them nowadays, and if yes, how?
  2. If not, could it be an improvement to a future TypeScript version? E.g:
class Person {
  constructor(public { firstName, lastName, age } : { firstName: string, lastName: string, age: number }) {

  }
}

// the code above would possibly transpile to:
var Person = (function () {
    function Person(_a) {
        var firstName = _a.firstName, lastName = _a.lastName, age = _a.age;
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }
    return Person;
})();

Activity

xLama

xLama commented on Oct 20, 2015

@xLama

I like this.

DanielRosenwasser

DanielRosenwasser commented on Oct 20, 2015

@DanielRosenwasser
Member

@buzinas I originally had this working in #1671, but didn't take it in (check out #1541). I think the issue was that it was relatively dense in semantics. I don't necessarily agree, but maybe someone else on the team can weigh in.

danquirk

danquirk commented on Oct 20, 2015

@danquirk
Member

Note that in those proposals the parameter property modifier applied to the entire binding pattern (simpler) as opposed to the original suggestion here which in theory supports different visibility modifiers per destructured element (not sure that level of specificity would be worth it).

buzinas

buzinas commented on Oct 20, 2015

@buzinas
Author

@danquirk For me, if it's easier for you to do the other way, I don't really care. In fact, that was my first try (e.g public {firstName, lastName, age}), and as soon as it didn't work, I tried to use on each property, and it didn't work too.

It would be great if we could support both (since not always we want to create properties for all the parameters, and when we want, it would be simpler to use public/private only once), but if it's easier to support only one approach, it will be great already.

Probably it's something that people will use more and more, since destructuring is awesome.

robianmcd

robianmcd commented on Nov 26, 2015

@robianmcd

just ran into this. I think either approach would satisfy most use cases. Hope to see this in a future version.

laurelnaiad

laurelnaiad commented on Dec 23, 2015

@laurelnaiad

The first thing I tried was:

  constructor(thisProps: { public myProperty: string, public myOtherProperty: number }) {
    // etc
  }

Something like this would be a nice-to-have.

jack-guy

jack-guy commented on Feb 26, 2016

@jack-guy

Definitely +1 this. The alternative tends to be... bulky.

albertywu

albertywu commented on Mar 23, 2016

@albertywu

👍

hrundik

hrundik commented on Apr 19, 2016

@hrundik

👍

aliatsis

aliatsis commented on Apr 27, 2016

@aliatsis

+1

lekev

lekev commented on May 13, 2016

@lekev

+1

andriilazebnyi

andriilazebnyi commented on Jun 13, 2016

@andriilazebnyi

+1

117 remaining items

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

    Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @oppianmatt@sukima@HipsterZipster@kibiz0r@rodrigolive

      Issue actions

        Combining destructuring with parameter properties · Issue #5326 · microsoft/TypeScript