create a generic function returning a function with the same signature

后端 未结 1 835
梦如初夏
梦如初夏 2021-01-05 14:33

In Typescript I want to create a function that will take a function and return a function with the same input-output. the function itself needs to be generic. so that it can

相关标签:
1条回答
  • 2021-01-05 15:12

    Here you go :

    function f<A extends Function>(a:A):A {
        var newFunc = (...x:any[]) => {
            console.log('('+x.join(',')+') => ', a.apply(undefined, x));
            return null;
        }
        return <any>newFunc;
    }
    
    function a(j:string, k:number): boolean {
        return j === String(k);
    }
    
    var b = f(a);
    
    b("1", 1);
    b("a", 2);
    b('123','123'); // ERROR
    
    0 讨论(0)
提交回复
热议问题