Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 尝试将函数包装到另一个函数中时出现Typescript错误_Reactjs_Typescript - Fatal编程技术网

Reactjs 尝试将函数包装到另一个函数中时出现Typescript错误

Reactjs 尝试将函数包装到另一个函数中时出现Typescript错误,reactjs,typescript,Reactjs,Typescript,我试图在将一个函数传递给一个库以供以后运行之前,将它包装到另一个函数中。我在尝试使用.apply()和扩展参数时遇到各种类型的Typescript错误 库要求我传递一个“options”对象,包括一个名为PromiseFn的函数,该函数将由库使用任意数量的参数调用 我需要将promiseFn包装在一个新函数中,该函数将在调用原始promiseFn之前和之后运行一些代码。新函数将是传递给库的函数。这就是我尝试过的 let newOptions = options if(options.promi

我试图在将一个函数传递给一个库以供以后运行之前,将它包装到另一个函数中。我在尝试使用.apply()和扩展参数时遇到各种类型的Typescript错误

库要求我传递一个“options”对象,包括一个名为PromiseFn的函数,该函数将由库使用任意数量的参数调用

我需要将promiseFn包装在一个新函数中,该函数将在调用原始promiseFn之前和之后运行一些代码。新函数将是传递给库的函数。这就是我尝试过的

let newOptions = options

if(options.promiseFn !== undefined){

    let newPromiseFn = async (...args: any[]) => {

        ... before code

        await options.promiseFn?.apply(this, ...args)

        ... before code

    }

    newOptions.promiseFn = newPromiseFn

}

const { data } = useAsync(newOptions)
我当前收到的typescript错误是

(参数)参数:任意[] 应为1-2个参数,但得到1个或多个。ts(2556)

它与此行中的args相关

await options.promiseFn?.apply(this, ...args)

非常感谢您的帮助。

如果我能很好地理解您的问题,您只需要使用一些可以包装您的函数的东西

我想你可以用这样的东西:

type GetReturnType<original extends Function> = original extends (...x: any[]) => infer returnType ? returnType : never;
type GetArgumentsType<originalArguments> = originalArguments extends (...args: infer formatArguments) => any ? formatArguments : never[];

const myEncapsulatedFunction = async <T extends Function>(
    func: T,
    ...args: GetArgumentsType<T>
): Promise<GetReturnType<T>> => wrappingFunction(func, args)();
type GetReturnType=原始扩展(…x:any[])=>推断返回类型?返回类型:从不;
键入GetArgumentType=originalArguments扩展(…参数:推断格式参数)=>any?格式参数:从不[];
const myEncapsulatedFunction=async(
func:T,
…args:GetArgumentsType
):Promise=>wrappingFunction(func,args)();
然后您可以像这样使用此包装器:

function wrappingFunction<T extends any[]>(func: Function, args: T) {
  // Do some stuff here
  await func(...args)
  // Do some stuff here
}

// Then
function doStuff(arg: string) {
  console.log(arg)
}


myEncapsulatedFunction(doStuff, 'hello');
函数包装函数(func:function,args:T){
//在这里做些事情
等待函数(…参数)
//在这里做些事情
}
//然后
函数doStuff(参数:字符串){
console.log(arg)
}
myEncapsulatedFunction(doStuff,'hello');

如果我能很好地理解您的问题,您只需要使用一些可以包装您的函数的东西

我想你可以用这样的东西:

type GetReturnType<original extends Function> = original extends (...x: any[]) => infer returnType ? returnType : never;
type GetArgumentsType<originalArguments> = originalArguments extends (...args: infer formatArguments) => any ? formatArguments : never[];

const myEncapsulatedFunction = async <T extends Function>(
    func: T,
    ...args: GetArgumentsType<T>
): Promise<GetReturnType<T>> => wrappingFunction(func, args)();
type GetReturnType=原始扩展(…x:any[])=>推断返回类型?返回类型:从不;
键入GetArgumentType=originalArguments扩展(…参数:推断格式参数)=>any?格式参数:从不[];
const myEncapsulatedFunction=async(
func:T,
…args:GetArgumentsType
):Promise=>wrappingFunction(func,args)();
然后您可以像这样使用此包装器:

function wrappingFunction<T extends any[]>(func: Function, args: T) {
  // Do some stuff here
  await func(...args)
  // Do some stuff here
}

// Then
function doStuff(arg: string) {
  console.log(arg)
}


myEncapsulatedFunction(doStuff, 'hello');
函数包装函数(func:function,args:T){
//在这里做些事情
等待函数(…参数)
//在这里做些事情
}
//然后
函数doStuff(参数:字符串){
console.log(arg)
}
myEncapsulatedFunction(doStuff,'hello');

promiseFn看起来像什么?它可以是传递给我中间层的任何函数,但根据typescripts,它看起来像-(属性)AsyncOptions.promiseFn?:promiseFn | Unfinedah,尝试删除
,所以
选项.promiseFn?.apply(this,args)
?不幸的是,我得到了-any[]类型的参数'不可分配给类型为“[props:AsyncProps,controller:AbortController]”的参数。目标需要2个元素,但源可能更少。ts(2345)
promiseFn
是什么样子的?它可以是传递到中间层的任何函数,但根据类型脚本,它看起来像-(属性)AsyncOptions。promiseFn?:promiseFn | unfinedah,尝试删除
,因此
选项。promiseFn?。应用(这个,args)
?不幸的是,我得到-any[]类型的参数不能分配给“[props:AsyncProps,controller:AbortController]”类型的参数。目标需要2个元素,但源的.ts(2345)可能更少