Skip to content

Assume type X = <T>() => Z<T>; as having an optionnal generic type parameter to enable deducing the return type on generic methodsΒ #57102

Closed as not planned
@denis-migdal

Description

@denis-migdal

πŸ” Search Terms

"ReturnType","generic method"

βœ… Viability Checklist

⭐ 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

  1. What do you want to use this for?

Get the returned type of a generic method.

  1. What shortcomings exist with current approaches?

There are no current approaches to my knowledge.

  1. What workarounds are you using in the meantime?

Couldn't find any. If you find some, I'm interested.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions