-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Simplify object destructuring in argument #14856
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
Comments
Sorry, seems like |
The flag name I understand your frustration with the current behavior. In addition to being ugly, I would say that the parameter destructuring renaming provided by Sadly, I don't think there's any getting around it. |
Yes, with Currently, I see only one solution - to not use object destructuring: Or introduce some "Interface Spread To Variables" operator which could be used as such: |
The guys at flow had a bit of a discussion about this same issue. I'm partial to this syntax: const MyComponent = ({
(aProp: string),
someProp: (renamedProp: number),
(onClick: (event: Event) => void),
}) => {} The syntax is highlighted in VSCode correctly which is nice, and it's invalid JS to use an expression inside destructuring, which means it won't conflict with backwards compatibility. I think it looks readable enough except for the implication you're defining a self-calling object or something as in a ts.d Or const MyComponent = ({
aProp as string,
someProp: renamedProp as number,
onClick as (event: Event) => void,
}) => {} |
As noted in #7576 (comment), the TC39 choice of syntax for this feature is unfortunate, and I feel your pain using it day in and day out. However, adding new non-standard syntax is not a good solution either. We would like to keep expression-level syntax as close as possible to the standard JS syntax. |
Also I would say this is a duplicate of #7576, with just new syntax proposal. |
Hello,
when doing some prototyping and playing with object destructuring in function argument I've noticed that I must duplicate identifier names in type information and if there are many variables it looks heavier:
function mainLayoutComponent({banners, profile, cart}: { banners: Component, profile: Component, cart: Component }): any { }
Proposal:
My suggestion would be that destructuring in function argument position would allow type information definition if object type is not explicitly defined:
function mainLayoutComponent({banners: Component, profile: Component, cart: Component}): any { }
It would be handy. I don't know how that sounds from language design perspective as it feels like it will be possible to define a function which argument identifier is "unknown" but with given type information.
Compiler could translate that kind of construct into destructuring syntax before compilation.
Note: I've seen propsal with
::
operator (#13471) which was rejected, but this "special treatment" of destructuring in argument space looks ES6, ES7 compatible.The text was updated successfully, but these errors were encountered: