eventemitter3 icon indicating copy to clipboard operation
eventemitter3 copied to clipboard

The EventEmitter3 library does not seem to support generic type merging

Open kamenomi-dev opened this issue 1 year ago • 1 comments

I initialize the class with the following code

import EventEmitter from "eventemitter3";

type defaultType = {
  "test"(): void;
};

class subClass<
  Z extends { [props: string]: (...args: any[]) => any },
> extends EventEmitter<Z & defaultType> {
  constructor() {
    super();

    let func = () => {};

    this.on("test", func);
    this.emit("test");
    this.off("test", func);
  }
}

But, in the following line of code:

this.on("test", func);
this.emit("test");
this.off("test", func);

There is an error in the parameters: Argument of type 'string' is not assignable to parameter of type 'EventNames<Z & defaultType>'.typescript(2345)

I don't know why, what will cause this problem? CodeSandbox: URL

kamenomi-dev avatar Jul 21 '24 15:07 kamenomi-dev

我有相同的问题,这个是 ts 的问题吗,说实话,不知道为什么会抛出这样的 ts 错误

zLikeMario avatar Aug 07 '24 05:08 zLikeMario