-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding IDE code completion support for custom event emitter functions? #241
Comments
hello
IMHO, gives pretty decent autocomplete experience My setup is
|
Hi, guys. I had the same problem with IDE support. So I found another solution - C#-like events. I use VSCode with @js-doc, no Typescript. There is an example how I define events: class Kitty {
/** @readonly */
emitter = new EventEmitter();
/**
* Event with 2 args: string, integer.
* @type {TypedEvent<[string, integer]>}
* @readonly
*/
eventSay = new TypedEvent(this.emitter, "say");
/**
* @param {string} word
* @param {integer} cnt
*/
say(word, cnt) {
this.eventSay.safeEmit(word, cnt); // editor shows the correct signature
}
}
const kitty = new Kitty();
kitty.eventSay.on((w, c) => { throw new Error('unhandled') }); // it will be ignored due to safeEmit
// editor shows the correct signature
kitty.eventSay.once((w, c) => console.log(`once say ${w}:${typeof(w)} ${c}:${typeof(c)}`));
kitty.eventSay.on((w, c) => console.log(`on say ${w}:${typeof(w)} ${c}:${typeof(c)}`));
kitty.say("meow", 3); Repo link: https://github.com/dgolovin-dev/eventemitter3-typedevent |
This is a bit old, but for others, here's how I solved it with TypeScript. import { EventEmitter } from "eventemitter3";
// The types of the arguments you'll pass
interface argOne {
foo: string;
}
interface argTwo {
bar: number;
}
// This will contain all possible events and the args those events will pass
interface MyCustomEvents {
"my.event": [argOne, argTwo];
}
// A new event emitter that uses the custom events
class TypedEmitter extends EventEmitter<MyCustomEvents> {
constructor(props) {
super();
}
doThing(text: string) {
const argA = {foo: text};
const argB = {bar: 1};
this.emit("my.event", argA, argB);
}
}
const test = new TypedEmitter();
test.on("my.event", (argOne, argTwo) => {
// argOne is of type argOne
// argTwo is of type argTwo
});
// My IDE will yell at me in these, for the respective reasons:
// no such event
test.on("doesnt.exist", () => {})
// that's not an operation I can perform on the type argOne is
test.on("my.event", (argOne) => {
argOne++;
});
// you don't receive that many args
test.on("my.event", (argOne, argTwo, argThree) => {
});
// It will also yell at me for the same reasons when using .emit()
|
I've been using this package for an event heavy project I'm working on and its been great to work with. Thanks :) - I am struggling to figure out a way to add my own emitter definitions so my IDE understands when I start typing the
.emit()
code it can help me by showing me what parameters it will accept. Based on what I have seen other people doing with other libraries and what I gathered from looking at your source code this is my best attempt:As you can see I haven't even been able to implement the
on*
method correctly since I still have theemit*
section commented out. This is as close as I can get it and I pretty much hit a wall. My compiler gives me the following complaint with this setup (when hoveringCEventEmitter
ofexport declare interface CEventEmitter
):I hope someone out there can see what I'm missing and help me out... I'm sorry if this is the wrong place for this post. I'm not sure where else I would put it besides stack overflow. I figured I would try here since this is kind niche. I'm not new to JavaScript but this is my first stab at a real TypeScript project so I'm trying to understand and learn how this works. Thank you in advance.
The text was updated successfully, but these errors were encountered: