-
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
Prefix <T> to access methods with generics : Klass[<T>"method"]
#57297
Comments
Workaround: access method from type Method = typeof Klass.prototype.method<number>; |
Nope. class Klass {
method<T>() {
}
}
// from the class
type T = Klass;
type m1 = T["method"]
// from the type of the class
type TK = typeof Klass;
type m2 = TK["prototype"]["method"]; Your solution could work using functions : function _getMethod<T extends BaseClass, U>(t: typeof T) {
return t.prototype.method<U>;
}
type getMethod<T extends BaseClass, U> = ReturnType<typeof _getMethod<T, U>> But due to Typescript widening, that will return cf #57286 Also, we could help TS by precising the return type : function _getMethod<T extends BaseClass, U>(t: typeof T): T["prototype"]["method"] {
return t.prototype.method<U>;
} But we have currently no ways to precise the generic parameters to the type... |
I don't understand what you're getting at here. @whzx5byb's answer isn't even a workaround; it's a solution. I also don't see what this being in a class has to do with anything. There are existing issues around generic instantiation of a type as opposed to a value. |
My suggestion wasn't about generic instantiation of a type, but to be able to fetch a generic version of a method.
But (1 operation):
Sorry if I'm not clear in my expression, I am not quite familiar with TS nomenclature and internal stuff. I then suggested an extension to the syntax for a more generic usage, that is indeed a generic instantiation of a type. I made another issue as other issues doesn't seem active / followed.
It isn't, as he gets the type from a value that happens to be a method, not from the class in TS. When manipulating types, there are lot of cases where you don't have access to the values : function foo<T extends Record<string, X>>(...) {
type T2<U> = {
[K in keyof T]: T[K][<U>"method"] // can't have access to the class value as we are in TS (type manipulation, not value manipulation).
}
} |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
generic methods
β Viability Checklist
β Suggestion
Use prefix
<T>
to access methods with generics:Could also be extended for all
<T>(...args: any[]) => any
types :I think there are no ambiguity with this syntax:
π Motivating Example
Currently, there are no ways to access a method with generics from a class.
This would solve numerous issues :
type X = <T>() => Z<T>;
as having an optionnal generic type parameter to enable deducing the return type on generic methodsΒ #57102π» Use Cases
Access methods with generics
No current approaches.
type X = <T>() => Z<T>;
as having an optionnal generic type parameter to enable deducing the return type on generic methodsΒ #57102 (comment) : won't work if the class isn't knownThe text was updated successfully, but these errors were encountered: