Allow overload signatures when writing class property initializers
See original GitHub issueSearch Terms
- typescript class bound function parameter overload
- typescript class bound function overload
- typescript class bound method overload
- typescript class bound arrow overload
Suggestion
It would be nice if we could overload bound arrow function methods in a class without the need to specifically declare a constructor and bind methods in it.
Use Cases
Examples
// existing valid implementation
// class `A` has method `a` bound to its instance in the constructor
class A {
constructor(){
this.a = this.a.bind(this);
}
a(a: string): string;
a(a: string, b: string): string;
a(a: string, b?: string): string {
if(b) return a + b;
return a;
}
}
// suggested alternative implementation
// class `B` declares a bound arrow function `a` and its overloads without the need for a ctor
class B {
a = (a: string): string;
a = (a: string, b: string): string;
a = (a: string, b?: string): string => {
if(b) return a + b;
return a;
}
}
Checklist
My suggestion meets these guidelines:
- 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, etc.)
- This feature would agree with the rest of TypeScript’s Design Goals.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:9
- Comments:8
Top Results From Across the Web
TypeScript function overloading - Stack Overflow
Only the three overloads are recognized by TypeScript as possible signatures for a method call, not the actual implementation. The ...
Read more >Documentation - Classes - TypeScript
A field declaration creates a public writeable property on a class: ... There are just a few differences between class constructor signatures and...
Read more >Object-oriented code - pybind11 documentation
Write only properties can be defined by passing nullptr as the input for the read ... To enable dynamic attributes for C++ classes,...
Read more >Init only setters - C# 9.0 draft specifications - Microsoft Learn
This proposal adds the concept of init only properties and indexers to C#. These properties and indexers can be set at the point...
Read more >Python Class Constructors: Control Your Object Instantiation
Class constructors are a fundamental part of object-oriented programming in Python. They allow you to create and properly initialize objects ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@zacnomore’s approach didn’t work for me.
What I did to resolve this:
You already can bind class methods with the arrow syntax in JS:
And you can overload normal class methods in TS:
But you can’t have both; you can’t have a bound method and overload it too (syntax error):
I was looking to achieve that ^