Skip to content
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

Double colon operator for more concise method binding #56038

Closed
5 tasks done
BribeFromTheHive opened this issue Oct 9, 2023 · 3 comments
Closed
5 tasks done

Double colon operator for more concise method binding #56038

BribeFromTheHive opened this issue Oct 9, 2023 · 3 comments

Comments

@BribeFromTheHive
Copy link

πŸ” Search Terms

"method reference", "double colon operator", ":: syntax", "Java-like method reference"

βœ… Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

This proposal aims to introduce a method reference syntax in TypeScript to make the language more intuitive and less prone to common errors related to function context. By adopting this feature, TypeScript could offer a cleaner, more logical way to handle method references, aligning with the language's goal of enhancing JavaScript with improved tooling.

πŸ“ƒ Motivating Example

A method reference syntax could offer a more intuitive way to pass methods as callbacks, making the code cleaner and less prone to common mistakes.

Desired TypeScript Example:

button.addEventListener('click', this::handleClick);

πŸ’» Use Cases

  1. What do you want to use this for?
    I aim to use this feature to simplify and improve the readability of code when passing methods as callbacks. This would be especially beneficial in scenarios where preserving the this context is crucial. By reducing boilerplate and eliminating redundancy, we can make the code more maintainable and less error-prone.

  2. What shortcomings exist with current approaches?

  • Confusing Syntax: The current syntax options, such as .bind(this) and arrow functions, can be confusing and counterintuitive. The redundancy in .bind(this) and the misleading appearance of arrow functions can lead to mistakes.

  • Missed Opportunity for Intuitiveness: TypeScript has the chance to adopt a syntax that makes it clear that a method reference is being passed without any additional logic or immediate invocation. This would align TypeScript more closely with functional programming paradigms and make the language more intuitive.

  1. What workarounds are you using in the meantime?
    In the absence of a method reference syntax, developers are left with suboptimal solutions like .bind(this) and arrow functions. These workarounds are functional, but fall short of the elegance and readability that could be achieved with a dedicated syntax for this purpose.
@xiBread
Copy link

xiBread commented Oct 9, 2023

https://github.com/tc39/proposal-bind-operator
https://github.com/tc39/proposal-extensions

@MartinJohns
Copy link
Contributor

MartinJohns commented Oct 9, 2023

Duplicate of #3508. Used search terms: bind in:title

aligning with the language's goal of enhancing JavaScript with improved tooling.

In general, because based on your issue you're not aware of it, the goal of TypeScript is not just to improve JavaScript with improved tooling, but to add type information to JavaScript - and only that. Adding syntax that is beyond the type-only level (like your suggestion) and does not exist in JavaScript is explicitly out of scope (as mentioned in the viability checklist).

@BribeFromTheHive
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants