-
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
Suggest syntactic sugar: extension method/function #45968
Comments
Duplicate of #9 (and others). Used search terms:
I really don't see how this could be implemented without emitting different JS. |
I don't believe that it is really a duplicate of #9. May be trying to achieve a similar thing. But it's a very simple implementation. Maybe I don't quite understand:
I don't see how things need to be different based on expression types..? Are you able to explain? |
If you don't think it's a duplicate of #9, then it's definitely still a duplicate of #24889. The very exact thing asked. And IMO it's a duplicate of #9. You just propose a different syntax, but the feature is the same.
You want different JS emitted depending on the declared extension methods. That's generally a no-go in TypeScript, as it goes beyond the type-system scope TypeScript aims at. |
Thanks for the clarification. Maybe I didn't explain well. The proposal isn't to emit different JS depending on the types of the expressions. Let me try again: Extension method declaration// proposal
extension meExtn(this: MyType): void {
// ...
}
// treated the same as typescript
function meExtn(this: MyType): void {
// ...
}
// which emitted in JS as something like this
function myExtn() {
// ...
} Extension method usage
// proposal
target.myExtn();
// treated the same as typescript
myExtn.bind(target)();
// which emitted in JS as something like this
myExtn.bind(target)(); So what's emitted is not based on the types of the expressions. It's just a simple re-arrangement. Obviously there would be some fancy type checking stuff that I won't try to pretend to understand. |
You're emitting different JS. |
It's still type-directed emit because when TS sees the function call expression |
Oh ok. Thanks for the explanation. I see that now. I had imagined that if you imported |
I liked this suggestion. I think the philosophy of language shouldn't be that uncompromising. We already have |
I have a lib that extends native types. This feature would fit like a glove. |
At least the addition of IMO that TypeScript focuses on a single specific goal is something I really like about the language. |
Which is still type-directed emit because it relies on the compiler knowing the type of This is just something that's outside the current design philosophy of the language, which is to be, essentially, "JavaScript with compile-time type checking". With the exception of grandfathered features like Note also that lots of workflows use |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Suggestion
Syntactic sugar implementation of extension methods/functions without polluting prototypes (or even requiring them at all, i.e. works for interfaces too)
Can be used:
Simply compiles to something like
So
this
in the extension can be any type/class/interface and it can be used on anything that matches.🔍 Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Basically it's a simple syntax transformation to make function binding pretty.
Could probably be use for type guards too
💻 Use Cases
Any utility library...
The text was updated successfully, but these errors were encountered: