Closed as not planned
Description
π Search Terms
"ReturnType","generic method"
β 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
Given the following types :
type X = <T>() => Z<T>;
// or
type X2<Y> = <T>(param: Y) => Z<T>;
I suggest we should be able to give a generic type parameter to the types :
type Xstr = X<string> ; // Xstr = <string>() => Z<string>
// or
type X2str<Y> = X2<Y><string> ; // X2str<Y> = <string>(param: Y) => Z<string>
π Motivating Example
When having classes with a generic method :
class Klass {
method<T>(): Z<T> { ... }
}
We can get the method as : type X = Klass["method"]
which would be type X = <T>() => Z<T>
.
However, to my knowledge, there are currently no ways to get the return type like :
type Return<T> = ReturnType<Klass["method"]<T>>; // expected : Return<T> = Z<T>
// currently gives an error : "; expected"
We can only do :
type Return = ReturnType<Klass["method"]>; // Return = Z<unknown>
However, it is possible to do :
type X = Klass["method"]>;
let tmp: X = {} as any; // h4ck for the demonstration
type Return<T> = ReturnType<typeof tmp<T>>; // Return<T> = Z<T>
π» Use Cases
- What do you want to use this for?
Get the returned type of a generic method.
- What shortcomings exist with current approaches?
There are no current approaches to my knowledge.
- What workarounds are you using in the meantime?
Couldn't find any. If you find some, I'm interested.