Skip to content

Prefix <T> to access methods with generics : Klass[<T>"method"]Β #57297

Closed as not planned

Description

πŸ” Search Terms

generic methods

βœ… Viability Checklist

⭐ 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 :

πŸ’» Use Cases

  1. What do you want to use this for?

Access methods with generics

  1. What shortcomings exist with current approaches?

No current approaches.

  1. What workarounds are you using in the meantime?

Activity

  1. whzx5byb commented on Feb 5, 2024

    @whzx5byb

    Workaround: access method from prototype.

    type Method = typeof Klass.prototype.method<number>;
  2. denis-migdal commented on Feb 5, 2024

    @denis-migdal
    Author

    Workaround: access method from prototype.

    type Method = typeof Klass.prototype.method<number>;

    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.

    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 BaseClass.prototype.method<U> instead of T.prototype.method<U>

    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...

  3. RyanCavanaugh commented on Feb 5, 2024

    @RyanCavanaugh
    Member

    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.

    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.

  4. denis-migdal commented on Feb 5, 2024

    @denis-migdal
    Author

    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.
    It is not (2 operations) :

    1. operator []: we fetch Klass["method"]
    2. operator <>: we make a generic instantiation of the returned type.

    But (1 operation):

    1. operator [<> ]: we fetch Klass[<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:

    • no syntax ambiguity (?)
    • simple, clear and concise syntax.
    • I guess not too much trouble to implement (?).

    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.

    I don't understand what you're getting at here. whzx5byb's answer isn't even a workaround; it's a solution.

    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).
         }
    }
  5. typescript-bot commented on Feb 8, 2024

    @typescript-bot
    Contributor

    This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. locked as resolved and limited conversation to collaborators on Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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