Repository navigation
Closed as not planned
Labels
DuplicateAn existing issue was already created
Description
🔍 Search Terms
generic methods
✅ Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
Use prefix <T> to access methods with generics:
class Klass {
method<T>() {}
}
type Method = Klass[<number>"method"] // <number>() => voidCould also be extended for all <T>(...args: any[]) => any types :
type Fct = <T>() => void;
type FctT<U> = <U>Fct; // FctT<U> = <U>() => void;I think there are no ambiguity with this syntax:
typeA<T>typeB // syntax error
<T>typeA.typeB // = (<T>typeA).typeB
typeA.<T>typeB // = <T>(typeA.typeB)📃 Motivating Example
Currently, there are no ways to access a method with generics from a class.
This would solve numerous issues :
- Assume
type X = <T>() => Z<T>;as having an optionnal generic type parameter to enable deducing the return type on generic methods #57102 - Introduce mechanism for manipulating function types without losing generic type information #50481
- Instantiation expressions #47607 (comment)
- Trying to get the type of a generic method of a class results in a parse error #53410
💻 Use Cases
- What do you want to use this for?
Access methods with generics
- What shortcomings exist with current approaches?
No current approaches.
- What workarounds are you using in the meantime?
- Assume
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 known - Introduce mechanism for manipulating function types without losing generic type information #50481 (comment) : requires to register in an interface all classes that will be used.
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already created
Activity
whzx5byb commentedon Feb 5, 2024
Workaround: access method from
prototype.denis-migdal commentedon Feb 5, 2024
Nope.
You didn't "access a method with generics from a class" in TS, but asked the type of a value that happens to be a method.
Your solution could work using functions :
But due to Typescript widening, that will return
BaseClass.prototype.method<U>instead ofT.prototype.method<U>cf #57286
Also, we could help TS by precising the return type :
But we have currently no ways to precise the generic parameters to the type...
RyanCavanaugh commentedon Feb 5, 2024
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.
denis-migdal commentedon Feb 5, 2024
My suggestion wasn't about generic instantiation of a type, but to be able to fetch a generic version of a method.
It is not (2 operations) :
[]: we fetchKlass["method"]<>: we make a generic instantiation of the returned type.But (1 operation):
[<> ]: we fetchKlass[<T>"method"]directly as a type.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.
Which would give (in the same case as above):
<T>(Klass["method"]). So it is slightly different.I made another issue as other issues doesn't seem active / followed.
I think I suggested a way that seems to not have the drawback of other cited suggestions:
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 :
typescript-bot commentedon Feb 8, 2024
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.