andywer / typed-emitter

🔩 Type-safe event emitter interface for TypeScript
MIT License
268 stars 24 forks source link

fix numbers as event names #38

Open sertonix opened 1 year ago

sertonix commented 1 year ago

Problem:

class Foo extends (EventEmitter as new () => TypedEmitter<{1: () => void}>) {
    constructor() {
        super();
        this.emit("1"); // error  should be no error
    }
}

After fix:

class Foo extends (EventEmitter as new () => TypedEmitter<{1: () => void}>) {
    constructor() {
        super();
        this.emit("1"); // no error
        this.emit(1); // no error
        // a number as event name also works in node but for some reason it is not documented like that
    }
}

class Bar extends (EventEmitter as new () => TypedEmitter<{
    "1": () => void,
}>) {
    constructor() {
        super();
        this.emit("1"); // no error
        this.emit(1); // no error

        // known issue:
        this.emit(3); // no error
        // I found no way to get the number from a string
        // but the arguments are typed correctly:
        this.emit(3,""); // error
    }
}

(redo from #33)