-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow accessors to support inline generics for this #35986
Comments
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. public getAllSettings<T extends this>(): Array<Setting<T>> {
return (allPropertiesOf(this) as Array<keyof T>)
.filter(prop => Reflect.getMetadata('setting', this, prop as string))
.map(prop => {
const metadata = Reflect.getMetadata('setting', this, prop as string);
return {
name: metadata.name,
controlType: metadata.controlType,
value: new Ref(this as T, prop)
};
});
} I need this because my method is defined in an abstract parent class. If I simply use |
Another use-case for this is writing custom logic when exporting modules, e.g. memoizing things. interface Item<T> {
f: (value: T) => number;
}
declare function createObj<T>(): Item<T>
const MEMO: Record<string, Item<any>> = {}
module.exports = {
// ⬇️ An accessor cannot have type parameters.(1094)⬇️
get Container<T>(): Item<T> {
if(!MEMO.Container) {
MEMO.Container = createObj<T>();
}
return MEMO.Container;
}
} We stumbled upon this issue in RNGH after we rewrote the library to TS and tried to type the |
This sounds great and would allow something like this: type Exact<T, SHAPE> = T extends SHAPE ? (Exclude<keyof T, keyof SHAPE> extends never ? T : never) : never;
set someData<T extends SliderImageSmall | SliderImage>(
value: (T extends SliderImageSmall ? Exact<T, SliderImageSmall> : Exact<T, SliderImage>)[],
) {
this.someData = value
} |
I would also love this. I hit this case when trying to use a mapped type to create static properties on a dynamic subclass. Here's an example (playground link) for a simple enum-like abstraction: declare class Simple<K extends string> {
match<U>(matcher: { [P in K]: () => U }): U;
}
type SimpleConstructor<K extends string> = {
new(key: K): Simple<K>;
}
type Class = abstract new(...args: any[]) => any;
type SimpleClass<K extends string> = SimpleConstructor<K> & {
[P in K]: <This extends Class>(this: This) => InstanceType<This>;
}
declare function Enum<K extends string[]>(...keys: K): SimpleClass<K[number]>;
class Bool extends Enum("True", "False") {}
let truthy = Bool.True();
let falsy = Bool.False();
let truthyTruth = ifTrue(truthy, () => "truthy!");
let falsyTruth = ifTrue(falsy, () => "truthy!");
function ifTrue<U>(bool: Bool, callback: () => U): U | void {
return bool.match({
True: callback,
False: () => undefined
})
} Because I am able to use a Since getters conceptually do have a |
Feels like a slightly related issue (especially based on that comment) is #22509 (comment) |
Search 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 genericthis
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.This pattern can currently be emulated by converting the accessors into standard methods
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: