You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
You can use type annotations with destructuring patterns, but only on the top-level pattern:
functionsomeFunc(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.
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.
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:
does not allow to use the signature as an implicit documentation for the function user.
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.
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: