Skip to content

Instantly share code, notes, and snippets.

@lordlycastle
Created December 27, 2021 15:58
Show Gist options
  • Select an option

  • Save lordlycastle/b3a24db5d05aa77547376afdae0f65d5 to your computer and use it in GitHub Desktop.

Select an option

Save lordlycastle/b3a24db5d05aa77547376afdae0f65d5 to your computer and use it in GitHub Desktop.

Revisions

  1. lordlycastle created this gist Dec 27, 2021.
    32 changes: 32 additions & 0 deletions ClassFunctionKeyMapper.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    class MyClass {
    public myProp = 13;
    public static readonly myStaticReadOnlyProp = 99;
    private myPrivateProp: string | undefined;

    constructor() {
    this.myPrivateProp = undefined;
    }

    public myPublicFunction() {
    return 1;
    }

    public myPublicFunction2(arg: string): Promise<void> {
    return Promise.resolve();
    }

    private myPrivateFunction() {return false;}

    }

    const keyOf: keyof MyClass = 'myPublicFunction2'
    type MyFunctions<T> = {
    [K in keyof T & string]: T[K] extends Function ? `func-${K}` : never
    }[keyof T & string]

    const funcName: MyFunctions<MyClass> = 'func-myPublicFunction2';
    const instance = new MyClass()
    function stubFunction<T extends MyClass>(instance: T, method: MyFunctions<T>){
    console.log(`class: ${instance} - ${method} : result ${instance[method]()}`);
    }