Generic type for all class methods names with return type

Krzysztof Kaczyński

I am attempting to create a generic which will give me information about all class methods return types (+ fields, but they are not that important).

Let's say I have created such a class:

class TestClass {
    testField: string;

    constructor() {
        this.testField = 'test';
    }

    testMethod1(param: number): number {
        return param;
    }

    testMethod2(param: string): string {
        return param;
    }

}

As the result, I would like to have such type:

interface TestClassMethodsNamesReturnType {
  testMethod1: number;
  testMethod2: string;
}

What I have tried so far

  • mapping types + types inference:

    Example 1

    export type ClassMethodsNamesReturnType<T extends Function> = {
        [k in keyof T['prototype']]: ReturnType<T['prototype'][k]>;
    }
    
    const x: ClassMethodsNamesReturnType<TestClass> = {
        //...
    }
    

    Error:

    Type 'TestClass' is missing the following properties from type 'Function': apply, call, bind, prototype, and 5 more

  • mapping types and treat class as object:

    Example 2

    // I think here I can have problems to decide is the key of my class is a field or method 
    // what can be a problematic to decide what I should use (typeof T[K] or ReturnType<typeof T[K]>)
    export type ClassMethodsNamesReturnType<T extends Record<string, T[K]>, K extends keyof T> = {
        [K]: T[K];
    }
    

    Error:

    A computed property name in a type literal must refer to an expression whose type is a literal type or a 'unique symbol' type

    'K' only refers to a type, but is being used as a value here

Do you have any hints or ideas how can I achieve this?

sno2

You can do this via mapped types although you would need to do a conditional filter using the as clause in the mapped type. Then, check if the value extends Function and then return never if it does not so it will not be mapped over. Here's the full code:

class TestClass {
    testField: string;

    constructor() {
        this.testField = 'test';
    }

    testMethod1(param: number): number {
        return param;
    }

    testMethod2(param: string): string {
        return param;
    }

}

type FnReturns<T> = { [K in keyof T as T[K] extends Function ? K : never]: ReturnType<T[K] extends (...args: any[]) => any ? T[K] : never> };

// Correctly passes:
const foo: FnReturns<InstanceType<typeof TestClass>> = {
    testMethod1: 23,
    testMethod2: "hey",
}

// correctly fails:
const fail: FnReturns<InstanceType<typeof TestClass>> = {}

TypeScript Playground Link

Also note how I make use of the InstanceType of typeof TestClass to get the instance methods and properties of TestClass.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Return Type of Java Generic Methods

From Dev

How to return class type of a generic class with generic?

From Dev

Transform return type of all function in the class while preserve generic

From Java

How to return the Class of a generic type

From Dev

Stream return class type generic

From Dev

Is it possible to force that all class public methods return the same type with typescript?

From Dev

Using type variable as a return type in a generic class

From Dev

How to wrap a class that has generic type methods with a class that has a generic type and no generic type arguments on the methods?

From Dev

Delegate for generic class and method with generic return type

From Dev

Java generic methods, wildcard List return type

From Dev

Generic type invocation using string class names

From Java

Is it possible to access methods of generic type in an interface class?

From Dev

Generic type parameters C# - How to generic class return type

From Java

Why type parameter required before return type for static generic methods

From Dev

Return class of generic type in default interface method

From Java

Return a Class instance with its generic type

From Dev

How to force type on return value in a generic class?

From Java

How to force type on return value in a generic class?

From Java

Getting class of return generic type possible?

From Dev

How to return generic class with opaque type

From Java

Method return type using enclosed Generic class?

From Dev

Typescript: Wrap function return type of generic class

From Dev

Methods with 2 generic types in class with 1 generic type

From Dev

Generic methods type inference

From Dev

Generic methods and type casting

From Dev

Infer Typescript class to return the generic type based of generic value

From Dev

Inherit from multiple classes and change the return type of all methods of one parent class

From Java

Spring AOP Doesn't Apply on Methods with Generic Type Return Values

From Java

Java Object return type vs. Generic Methods

Related Related

  1. 1

    Return Type of Java Generic Methods

  2. 2

    How to return class type of a generic class with generic?

  3. 3

    Transform return type of all function in the class while preserve generic

  4. 4

    How to return the Class of a generic type

  5. 5

    Stream return class type generic

  6. 6

    Is it possible to force that all class public methods return the same type with typescript?

  7. 7

    Using type variable as a return type in a generic class

  8. 8

    How to wrap a class that has generic type methods with a class that has a generic type and no generic type arguments on the methods?

  9. 9

    Delegate for generic class and method with generic return type

  10. 10

    Java generic methods, wildcard List return type

  11. 11

    Generic type invocation using string class names

  12. 12

    Is it possible to access methods of generic type in an interface class?

  13. 13

    Generic type parameters C# - How to generic class return type

  14. 14

    Why type parameter required before return type for static generic methods

  15. 15

    Return class of generic type in default interface method

  16. 16

    Return a Class instance with its generic type

  17. 17

    How to force type on return value in a generic class?

  18. 18

    How to force type on return value in a generic class?

  19. 19

    Getting class of return generic type possible?

  20. 20

    How to return generic class with opaque type

  21. 21

    Method return type using enclosed Generic class?

  22. 22

    Typescript: Wrap function return type of generic class

  23. 23

    Methods with 2 generic types in class with 1 generic type

  24. 24

    Generic methods type inference

  25. 25

    Generic methods and type casting

  26. 26

    Infer Typescript class to return the generic type based of generic value

  27. 27

    Inherit from multiple classes and change the return type of all methods of one parent class

  28. 28

    Spring AOP Doesn't Apply on Methods with Generic Type Return Values

  29. 29

    Java Object return type vs. Generic Methods

HotTag

Archive