-
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
Proposal: Supplementing Implicit Types #19550
Comments
This is definitely the most interesting solution to the destructuring arguments typing problem that I've seen proposed. |
@aluanhaddad Cool! I don't know how the typescript code-base or how it works in the engine. Is it easy to access the known type at the time the type expression is being processed? If the known type is already available, I would think it would be easy to accomplish what I described (extending the known type with a supplemental description). Some other points:I think this pattern would be preferable to a union in many cases. (Whenever it is desired to modify the type of a member instead of resulting in a union with the existing member type). It also compliments Mapped types well. Mapped types affect all members, whereas this could affect only targeted members. |
Any feedback on this concept? Is there a better place to promote this idea? |
The in-place typing is shorter, easier to think through, and less repetitive. They closed this idea out before, perhaps over syntax, but I don't see why // Previous example
const { req, opt = '', def = 'DEFAULT' }: extends { req: string } = values;
// Just simple in-place typing
const { req is string, opt = '', def = 'DEFAULT' } = values; |
@tjpalmer Yeah, that could work for function parameters. However, I would think “as” would make more sense because “as” indicates type coercion, whereas “is” indicates type comparison. |
I suggested |
Ah, yeah I think ECMAscript destructured renaming is the worst design decision I have seen in modern languange design (especially when they knew very well that this would fly in the face of TypeScript). Like what were they thinking... ‘’’ // And this is clear // And we need Default values // What about renaming? // This would be too easy // Oh, I know, let’s just reverse them because it makes sense - after all we have exactly no examples in the entire languange where a value assignment flows from left to right. So let’s just go in reverse from expected order for no good reason at all. // Not only will this send a clear sign to TypeScript that we can do whatever we feel like, it will bring back the good old days when everyone made fun of horrible JavaScript design decisions. I don’t think that was the exact thought process, but I just don’t get it. |
In a type expression, I would like to be able to extend the implicitly known type of a value with specific type information.
The purpose is to get the most from the implicit type information and suplement it when some type information is missing.
Also, this will pull typescript even closer to ES6 by allowing common ES6 patterns like argument destructuring which results in the any type (where the type cannot currently be specified).
Syntax
The extends keyword would only modify the type information for the part of the type expression that was specified.
Useful Cases
Argument Destructuring with Type Information
Refer to: #7576 (comment)
And my comment there: #7576 (comment)
Full Typing (Absolutely no advantage from implicit type information)
Current Best Solution
This uses Pure Implicit Typing, but it requires a default parameter that allows an unintended invalid call.
With Extends
Extending Return Values
Current Best Solution
With Extends
Checklist
Syntactic
Semantic
Emit
Compatibility
Other
The text was updated successfully, but these errors were encountered: