Allow accessors to support inline generics for this
See original GitHub issueSearch Terms
getter, accessor, this, generic
Suggestion
Currently accessor declarations do not support generics at the declaration site. The error returned is An accessor cannot have type parameters.ts(1094)
. Currently the accessor can implement generic declaration via the enclosing class. The goal would be to allow for the accessor to support a generic this
at the declaration site.
Use Cases
The use case is to allow for more expressive setter/getter declarations, in line with how method declarations currently work.
Examples
Suggested Pattern
Below is an example of defining the constraints of this
at the accessor site, to determine if an inherited accessor method is valid by the shape of the subclass.
class Base {
get left<T extends { _left: string}>(this: T) {
return this._left;
}
get right<T extends { _right: string}>(this: T) {
return this._right;
}
}
...
class Left extends Base {
_left: string = '';
}
new Left().left; // OK
new Left().right; // Errors
...
class Right extends Base {
_right: string = '';
}
new Right().right; // OK
new Right().left; // Errors
...
class Both extends Base {
_right: string = '';
_left: string = '';
}
new Both().right; // OK
new Both().left; // OK
This pattern can currently be emulated by converting the accessors into standard methods
class Base {
getLeft<T extends { _left: string}>(this: T) {
return this._left;
}
getRight<T extends { _right: string}>(this: T) {
return this._right;
}
}
...
class Left extends Base {
_left: string = '';
}
new Left().getLeft(); // OK
new Left().getRight(); // Errors
...
class Right extends Base {
_right: string = '';
}
new Right().getRight(); // OK
new Right().getLeft(); // Errors
...
class Both extends Base {
_right: string = '';
_left: string = '';
}
new Both().getRight(); // OK
new Both().getLeft(); // OK
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:36
- Comments:6 (1 by maintainers)
I would love that feature, too. I would like to use getters for “modifiers” without arguments, that return a modified object of the same type. E.g.:
In this particular case, we could return type A, but as soon as we start with inheritance:
the type of b would be converted to A.
I agree with the need for this. The use case I ran in to today required me to change my getter to a method, which is annoying as it’s inconsistent with the rest of my code and I’m only doing it to support the type parameters.
I need this because my method is defined in an abstract parent class. If I simply use
keyof this
it only allows for keys in the parent class and not the subclasses that inherent from it.