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

Support for object literal property value shorthand #418

Closed
maryo opened this issue Aug 10, 2014 · 6 comments
Closed

Support for object literal property value shorthand #418

maryo opened this issue Aug 10, 2014 · 6 comments
Labels
ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue Suggestion An idea for TypeScript

Comments

@maryo
Copy link

maryo commented Aug 10, 2014

Started here: https://typescript.codeplex.com/workitem/1367

Please consider adding support for object literal property value shorthand, as per the ES6 proposal: http://wiki.ecmascript.org/doku.php?id=harmony:object_literals#object_literal_property_value_shorthand. This would allow for the following:

function foo(a: number) {
  var b = 123;
  return { a, b };  // instead of { a: a, b: b };
}
@basarat
Copy link
Contributor

basarat commented Aug 11, 2014

👍

@danquirk
Copy link
Member

If ES6 is doing this then we'll certainly align with that. If it doesn't make it into ES6 then we'd almost assuredly have to wait for a future ES version lest we create incompatible syntax.

@mpawelski
Copy link

👍

With this feature this code:

var bigObject = {
    A: "1",
    B: "2",
    C: "3"
};

var smallObject = {
    A: bigObject.A,
    C: bigObject.C
}

could be written like this:

var bigObject = {
    A: "1",
    B: "2",
    C: "3"
};

var smallObject = {
    bigObject.A,
    bigObject.C
}

In my codebase this feature would very useful, because I'm often creating "smaller" object from "bigger" one, so for loop on "smaller" object enumerates just the properties I need.

@mhegazy
Copy link
Contributor

mhegazy commented Sep 25, 2014

@mpawelski I do not think this is what is proposed for ES6. the ES6 proposal is just for identifier names and not for dotted names.

@Arnavion
Copy link
Contributor

Arnavion commented Apr 9, 2015

Fixed by #1127 and #1346

@DanielRosenwasser DanielRosenwasser added Fixed A PR has been merged for this issue ES6 Relates to the ES6 Spec and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 9, 2015
@basarat
Copy link
Contributor

basarat commented Apr 10, 2015

In my codebase this feature would very useful, because I'm often creating "smaller" object from "bigger" one, so for loop on "smaller" object enumerates just the properties I need.

You can use destructuring with object literal property value:

var bigObject = {
    A: "1",
    B: "2",
    C: "3"
};

var {A,B} = bigObject,
    smallObject = {A,B};

🚨 untested code

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants