简体   繁体   English

定义我想作为输入的 function 类型:Typescript

[英]Defining type of function which I want to take as an input : Typescript

I want to make sure that my input accepts only below function as an argument:我想确保我的输入仅接受低于 function 作为参数:

  async getAll<T extends Entity>(url: string, params?: any): Promise<T[]> {
     // func body
  }

My function :我的 function

  setFunc(getAllFunc: any){
    this.func = getAllFunc;
  }

I want to replace any with custom type.我想用自定义类型替换any

Thanks in advance.提前致谢。

You can either use an interface or a type for this.您可以为此使用接口或类型。

Type:类型:

type TGetAllFunc<T extends Entity> = (url: string, params? : any) => Promise<T[]>;

Interface:界面:

interface IGetAllFunc<T extends Entity> {
    (url: string, params? : any): Promise<T[]>;
}

Then you can pass in either one to the function(I passed in the Interface here):然后你可以将其中一个传递给函数(我在这里传递了接口):

setFunc(getAllFunc: IGetAllFunc<Entity>){
  this.func = getAllFunc;
}

Based on your function the type of your getAll function is as follows:根据您的 function 您的getAll function 的类型如下:

type FunctionType<T> = (string, any) => Promise<T[]>;

You can then rewrite your function setFunc to take in the function type.然后,您可以重写您的 function setFunc以采用 function 类型。

setFunc(getAllFunc: FunctionType<T>){
    this.func = getAllFunc;
}

It seems like you set getAllFunc to class property, so that means that you would need to have your class also generic.似乎您将getAllFunc设置为 class 属性,这意味着您需要让您的 class 也是通用的。 so your class would look something like this,所以你的 class 看起来像这样,

type FunctionType<T> = (string, any) => Promise<T[]>;

class SomeClass<T> { // named SomeClass for brevity, use your own name here
    func: FunctionType<T>

    setFunc(getAllFunc: FunctionType<T>){
        this.func = getAllFunc;
    }

    /* ... */
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我想使用文件缓冲区调用函数时,我必须使用哪种打字稿类型? - Which typescript type do I have to use when I want to call a function with file buffer? 打字稿我想要一个类型,它是所有类型的联合属性 - Typescript I want a type which is the union of all types in an abject properties 定义TypeScript变量类型函数或字符串 - Defining TypeScript variable type function or string TypeScript函数,该函数返回与输入相同类型的值 - TypeScript function, which returns value of the same type as it's input 如何在 Typescript 中指定我想要的重载函数? - How to specify which overloaded function I want in Typescript? 为 function 定义类型,将参数添加到输入类型 - Defining type for function that adds parameters to input type Typescript - Function 返回类型如果我想返回 object - Typescript - Function return type if I want to return object TypeScript:是否可以创建一个使用泛型类型的函数来确定它可以将对象的哪个键作为其参数 - TypeScript: Is it possible to make a function that uses a generic type to determine which key of the object it can take as its params TypeScript:我可以使用类型泛型来返回不同的值并避免两次定义相同的函数吗? - TypeScript: Can I use type generics to return a different value and avoid defining the same function twice? 在函数类型中定义返回类型时打字稿额外的返回属性 - Typescript extra return properties when defining return type in function type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM