microsoft / microsoft/TypeScript
Allow overload signatures when writing class property initializers
Open
Nobody has claimed this yet.
Awaiting More Feedback
Suggestion
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
Search 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No source file or test entry point is named; start by reviewing the proposed class-property overload examples and the existing constructor-bound overload example. Done would mean TypeScript accepts the suggested syntax, preserves the intended type checking, and emits the expected JavaScript without requiring a constructor.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100