英文:
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<T extends (...args: any) => any>(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<T extends (...args: any) => any>(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) => 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)){
//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]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论