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

Destructuring variable declarations with types #5034

Closed
schungx opened this issue Sep 30, 2015 · 3 comments
Closed

Destructuring variable declarations with types #5034

schungx opened this issue Sep 30, 2015 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@schungx
Copy link

schungx commented Sep 30, 2015

There doesn't seem to be a way to specify type information on destructuring variable declarations with types. For example:

function someFunc(data: any) {
    const { x:string, y:number, z:boolean } = data;
}

This will be interpreted, as per ES6 rules, as the field data.x being assigned to a variable named string and so on.

The alternative syntax discussed before in another issue:

function someFunc(data: any) {
    const { x::string, y::number, z::boolean } = data;
}

did not make it into the current version.

Currently, you need to use initializers to fudge the typing info:

function someFunc(data: any) {
    const { x="", y=0, z=false } = data;
}

but then it becomes weird if you want a different default value:

function someFunc(data: any) {
    const { x=null as string, y=null as number, z=null as boolean } = data;
}

Can we put back a way to declare the types of destructuring pieces? This is very useful especially when integrating with legacy code, where there is limited type information upstream.

@ahejlsberg
Copy link
Member

You can use type annotations with destructuring patterns, but only on the top-level pattern:

function someFunc(data: any) {
    const { x, y } : { x: number, y: number } = data;
}

It's not the greatest, but at least it is clear what is going on. I'm concerned that proposals to introduce more overloaded meanings for : or as in destructuring patterns just make matters worse.

@schungx
Copy link
Author

schungx commented Sep 30, 2015

Not the greatest, since it requires double-typing the property names which may lead to errors before 1.6. 1.6 will now catch the mismatched property names.

But I hear you about over-over-loading :.

@schungx schungx closed this as completed Sep 30, 2015
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Sep 30, 2015
@RobertoMalatesta
Copy link

RobertoMalatesta commented May 12, 2016

At @ahejlsberg sorry to bug you on an already closed issue,
but having naming parameters would be a boon expecially when having functions/methods with more than an optional parameter.

ES6 destructuring does not fit the bill since having to pass a single data parameter and then destructuring:

  1. does not allow to use the signature as an implicit documentation for the function user.
  2. forces the implementer to unpack the data

C# has it and it's one of the things I miss most in Java (I would set naming parameters mandatory sometimes).

If the problem with them is the overloading of " : ", cannot we use, say, " := " ?

To avoid run-time performance, naming a parameter could only be accepted in a call where all parameters are named and passed, thus allowing the compiler to rearrange them.

Thanks for 30+yrs of good compilers -- RM

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants