-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Comments
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 |
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 |
At @ahejlsberg sorry to bug you on an already closed issue, ES6 destructuring does not fit the bill since having to pass a single data parameter and then destructuring:
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 |
There doesn't seem to be a way to specify type information on destructuring variable declarations with types. For example:
This will be interpreted, as per ES6 rules, as the field
data.x
being assigned to a variable namedstring
and so on.The alternative syntax discussed before in another issue:
did not make it into the current version.
Currently, you need to use initializers to fudge the typing info:
but then it becomes weird if you want a different default value:
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.
The text was updated successfully, but these errors were encountered: