Closed as not planned
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>() => void
Could 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.