-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Suggestion: Support type assertion on destructuring syntax #18229
Comments
Not sure how I feel about this proposal... But I would suggest using the It could look like: const { age as number, name as string } = person; Where the type of |
@rozzzly Generally using |
This is essentially a duplicate of #1912. |
@kitsonk Didn't find a solution to relieve the pain point on casting type on value nested in object. May a simple workaround or a new sound syntax cloud be discussed. |
I understand (from this and the previous issues) that there is still room for discussion on the syntax of this operation, but I think it would be really great to have this feature, as something that a lot of developers have become accustom to. Personally I feel the best approach would be:
But regardless of syntax, I'd love to see this feature in a TS release! You all have done such a great job at making our lives easier with a more structured and flexible programming language. |
That conflicts with ES spec of assigning to new variable names when destructuring. Breaking that for everyone would result in lots of tears. Because there is no straight forward syntax is the main reasons why the current methods are likely here to stay. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Code
Maybe we need a convenient way to add assertions on variables which are destructured out.
Nowadays, the workarounds are:
The first is a burden to rewrite the
obj
's type when its type is nested and complicated. The second seems weird.I'm assuming such a syntax like:
can absolutely help us asserting the type no matter how big the
obj
's type is.The text was updated successfully, but these errors were encountered: