如何在 TypeScript 中将函数数组传递给高阶组件函数而不丢失初始类型?

huangapple go评论57阅读模式
英文:

How to pass array of functions to HOC function without losing initial typing in typescript?

问题

在一些研究后,我设法弄清楚了一些函数,比如 useCallBack 是如何处理一个函数,对其进行处理,然后返回你的函数,而不会影响类型,所以你不会因为参数而感到困惑的。

这是我在第一个场景中想出来的,在只传递一个函数的情况下。

function doMagicToCallBackOrCallBacks<T extends (...args: any) => any>(arg: T){
    //进行处理
    return arg
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

但这并不是我需要的,实际上我还想添加另一种情况,其中我传递一个函数数组,然后得到相同顺序的数组,并且每个函数知道它的类型和预期的参数。

所以我尝试了这个,但是它不起作用。

function doMagicToCallBackOrCallBacks<T extends (...args: any) => any>(arg: T | T[]){
    if(Array.isArray(arg)){
        //对每个函数进行处理
        return arg as T[];
    }
    else {
        //对单个函数进行处理
        return  arg as T;
    }
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

const [firstFunction,secondFunction] = doMagicToCallBackOrCallBacks([basicFunction,anotherFunction]);
英文:

After some digging I managed to figure out how some functions like useCallBack manage to take a function, do something with it, and then return your function, WITHOUT messing with the typing, so you won't get confused about the arguments you used to require.

this is what i came up with in my first scenario, in case of a single function being passed.

function doMagicToCallBackOrCallBacks&lt;T extends (...args: any) =&gt; any&gt;(arg: T){
    //do magic
    return arg
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

but thats not quite what i need, i actually also want to add in another scenario, where i pass an array of functions, and get back the same array with the arrays in the same order and have each function know its type and expected arguments.

so i tried this, but it didnt work.


function doMagicToCallBackOrCallBacks&lt;T extends (...args: any) =&gt; any&gt;(arg: T | T[]){
    if(isArray(arg)){
        //do magic to each
        return arg as T[];
    }
    else {
        //do magic to one
        return  arg as T;
    }
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

const [firstFunction,secondFunction] = doMagicToCallBackOrCallBacks([basicFunction,anotherFunction]);

thanks!

答案1

得分: 1

type FnType =  (...args: any) => any

function doMagicToCallBackOrCallBacks<T extends FnType>(arg: T): T
function doMagicToCallBackOrCallBacks<T extends FnType[]>(arg: T): T
function doMagicToCallBackOrCallBacks(arg: FnType[] | FnType){
    if(Array.isArray(arg)){
        //对每个函数执行魔法
        return arg 
    }
    else {
        //对一个函数执行魔法
        return  arg 
    }
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

const [firstFunction,secondFunction] = doMagicToCallBackOrCallBacks([basicFunction,anotherFunction]);
英文:
type FnType =  (...args: any) =&gt; any

function doMagicToCallBackOrCallBacks&lt;T extends FnType&gt;(arg: T): T
function doMagicToCallBackOrCallBacks&lt;T extends FnType[]&gt;(arg: T): T
function doMagicToCallBackOrCallBacks(arg: FnType[] | FnType){
    if(Array.isArray(arg)){
        //do magic to each
        return arg 
    }
    else {
        //do magic to one
        return  arg 
    }
};


function basicFunction(number:number){
    return number.toString();
}
function anotherFunction(){
    
}

const basicFunctionButWithMagic = doMagicToCallBackOrCallBacks(basicFunction);

const [firstFunction,secondFunction] = doMagicToCallBackOrCallBacks([basicFunction,anotherFunction]);

huangapple
  • 本文由 发表于 2023年2月20日 00:50:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501769.html
  • typescript

我可以将 “ :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定