简体   繁体   English

如何在typescript中实现一个具有多个匿名函数的接口

[英]How to implement an interface with multiple anonymous functions in typescript

I am trying to implement interface shown below in typescript我正在尝试在 typescript 中实现如下所示的接口

interface A{
  (message: string, callback: CustomCallBackFunction): void;
  (message: string, meta: any, callback: CustomCallBackFunction): void;
  (message: string, ...meta: any[]): void;
  (message: any): void;
  (infoObject: object): void;
} 

here CustomCallBackFunction defintion这里 CustomCallBackFunction 定义

type CustomCallBackFunction= (error?: any, level?: string, message?: string, meta?: any) => void;

I am unable to implement the interface with class.我无法用 class 实现接口。 Any idea how to do this.任何想法如何做到这一点。

This is for method overloading For example, i have a class B with a variable of type A with implementation referring to all other options.这是用于方法重载例如,我有一个 class B 和一个类型为 A 的变量,其实现引用所有其他选项。

class B {
public c: A
}

Then i can call然后我可以打电话

const b =new B();
b.c(message,callback);
b.c(message,meta,callback);
b.c(message,meta);
b.c(infoObject);

Sadly, the only way to implement this interface to to make all arguments any .可悲的是,实现此接口的唯一方法是使所有 arguments 成为any

This interface is much like function overloads.这个接口很像function 重载。 Which means the implementation function must take an argument that is a union of all possibilities of the argument at that position.这意味着实现 function 必须采用一个参数,该参数是该 position 参数的所有可能性的联合。

  1. The first argument is string , string , string , any or object .第一个参数是stringstringstringanyobject So the type is string | object | any所以类型是string | object | any string | object | any string | object | any , which simplifies to just any since any includes the other possibilities. string | object | any ,它简化为any因为any包括其他可能性。

  2. The second argument is CustomCallBackFunction | any | undefined第二个参数是CustomCallBackFunction | any | undefined CustomCallBackFunction | any | undefined CustomCallBackFunction | any | undefined , which again is any CustomCallBackFunction | any | undefined ,这又是any

  3. The third argument could be CustomCallBackFunction or the second item of ...meta: any[] , so again, that one is any .第三个参数可以是CustomCallBackFunction...meta: any[]的第二个项目,所以再一次,那个是any


So, given that all arguments must be of type any , and there may any number of arguments, I think the only implementation signature that will work is:因此,鉴于所有 arguments 必须是any类型,并且可能有任意数量的 arguments,我认为唯一可行的实现签名是:

const fn: A = (...args: any[]) => {}

You'd then have to test the type of each argument yourself and figure out what the meaning of each one is and which call signature was used.然后,您必须自己测试每个参数的类型,并弄清楚每个参数的含义以及使用了哪个调用签名。 And yeah, that's gonna suck.是的,那会很糟糕。

const fn: A = (...args: any[]) => {
    if (args.length === 2 && typeof args[0] === 'string' && typeof args[1] === 'function') {
        // (message: string, callback: CustomCallBackFunction): void;
        const [message, callback] = args as [string, CustomCallBackFunction]
    } else if (args.length === 3 && typeof args[0] === 'string' && !Array.isArray(args[1]) && typeof args[2] === 'function') {
        // (message: string, meta: any, callback: CustomCallBackFunction): void;
        const [message, meta, callback] = args as [string, any, CustomCallBackFunction]
    } else {
        // etc...
    }
}

Playground 操场

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM