简体   繁体   English

Typescript 中的泛型函数映射

[英]Map of generic functions in Typescript

I've got a Message interface that takes a generic argument T and sets the value of T to be the type of an internal property called body :我有一个Message的界面,需要一个通用的说法T ,并设置的值T被称为内部属性的类型body

interface Message<T> {
  body: T
}

I also have an EventHandler interface that describes a generic function that takes a Message as it's sole parameter:我还有一个EventHandler接口,它描述了一个将Message作为唯一参数的通用函数:

interface EventHandler<T> {
  (event: Message<T>): void
}

Finally, I have an Integration class that has a subscribe method and a subscriptions map that both look like the following:最后,我有一个Integration类,它有一个subscribe方法和一个subscriptions映射,它们都如下所示:

class Integration {
  public subscriptions: new Map<string, EventHandler>()
  protected subscribe<T>(eventName: string, handler: EventHandler<T>) {
    this.subscriptions.set(eventName, handler)
  }
}

Ultimately, I want the user to be able to define their own Integration instances like this:最终,我希望用户能够像这样定义自己的Integration实例:

interface MyEventProperties {
  foo: string
}

class MyIntegration extends Integration {
   constructor() {
     super()
     this.subscribe('My Event Name', this.myEventHandler)
   }
   myEventHandler(event: Message<MyEventProperties>) {
     // do something
   }
}

The problem is that this doesn't work.问题是这行不通。 Because I don't know the generic type that ALL EventHandler functions will be getting I can't define the second generic parameter when instantiating the Map :因为我不知道所有EventHandler函数将获得的泛型类型,所以在实例化Map时我无法定义第二个泛型参数:

class Integration {
  public subscriptions: new Map<string, EventHandler>()
  // This errors with: Generic type 'EventHandler<T>' requires 1 type argument(s).

  protected subscribe<T>(eventName: string, handler: EventHandler<T>) {
    this.subscriptions.set(eventName, handler)
  }
}

My question is, am I approaching this problem incorrectly or am I missing something more obvious?我的问题是,我是否错误地处理了这个问题,还是遗漏了一些更明显的东西?

The simple solution is to use unknown as the generic parameter in the map.简单的解决方案是使用unknown作为地图中的通用参数。 The below solution will work:以下解决方案将起作用:

interface Message<T> {
    body: T
}
interface EventHandler<T> {
    (event: Message<T>): void
}

class Integration {
    public subscriptions = new Map<string, EventHandler<unknown>>()
    protected subscribe<T>(eventName: string, handler: EventHandler<T>) {
        this.subscriptions.set(eventName, handler)
    }
    public publishEvent<T>(eventName: string, msg: Message<T>) {
        (this.subscriptions.get(eventName) as EventHandler<T>)(msg);
    }
}

interface MyEventProperties { foo: string }
class MyIntegration extends Integration {
    constructor() {
        super()
        this.subscribe('My Event Name', this.myEventHandler)
        this.subscribe('My Event Name Mistaken', this.myEventHandler) // no relation between name and event 
    }
    myEventHandler(event: Message<MyEventProperties>) {
        // do something
    }
}

The problem though is there is no relation between the event name and the type of the event.但问题是事件名称和事件类型之间没有关系。 This may be a problem both when subscribing (as highlighted above) but also when emitting the event (the message body would not be checked against the expected message type)这可能是订阅时(如上突出显示)和发出事件时(不会根据预期的消息类型检查消息正文)时出现的问题

I would suggest adding an mapping type between event type and name:我建议在事件类型和名称之间添加一个映射类型:

interface Message<T> {
    body: T
}
interface EventHandler<T> {
    (event: Message<T>): void
}

class Integration<TEvents> {
    public subscriptions = new Map<PropertyKey, EventHandler<unknown>>()
    protected subscribe<K extends keyof TEvents>(eventName: K, handler: EventHandler<TEvents[K]>) {
        this.subscriptions.set(eventName, handler)
    }
    public publishEvent<K extends keyof TEvents>(eventName: K, msg: Message<TEvents[K]>) {
        (this.subscriptions.get(eventName) as EventHandler<TEvents[K]>)(msg);
    }
}

interface MyEventProperties { foo: string }

class MyIntegration extends Integration<{
    'My Event Name': MyEventProperties
}> {
    constructor() {
        super()
        this.subscribe('My Event Name', this.myEventHandler)
        this.subscribe('My Event Name Mistaken', this.myEventHandler) // error now
        this.subscribe('My Event Name', (e: Message<string>) => { }) // error, type of body not ok  
    }
    myEventHandler(event: Message<MyEventProperties>) {
        // do something
    }
}

let m = new MyIntegration();
m.publishEvent("My Event Name", 0) // error body not ok 
m.publishEvent("My Event Name M") // error mistaken name
m.publishEvent("My Event Name", {
    body: { foo: ""}
}) // ok message and body correct

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

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