Skip to content

Adding IDE code completion support for custom event emitter functions? #241

@RickKukiela

Description

@RickKukiela

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:

"use strict";

import {EventEmitter, EventListener, EventNames} from "eventemitter3";

export interface CEvents {
    'before-request': any[]; // based on your comments in the source code on extending interface EventTypes
}

export declare interface CEventEmitter {
    on<T extends EventNames<CEvents>>(
        event: T,
        fn: EventListener<CEvents, T>,
        context?: any
    ): this;

    // emit
}

export class CEventEmitter extends EventEmitter {
    constructor() {
        super();
    }
}

As you can see I haven't even been able to implement the on* method correctly since I still have the emit* 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 hovering CEventEmitter of export declare interface CEventEmitter):

TS2430: Interface 'CEventEmitter' incorrectly extends interface 'EventEmitter<string | symbol, any>'.

Types of property 'on' are incompatible.
    Type '<T extends "before-request">(event: T, fn: (...args: ArgumentMap<EventTypes>[Extract<T, "before-request">]) => void, context?: any) => this' is not assignable to type '<T extends string | symbol>(event: T, fn: (...args: any[]) => void, context?: any) => this'.
      
    Types of parameters 'event' and 'event' are incompatible.
        Type 'T' is not assignable to type '"before-request"'.           
        Type 'string | symbol' is not assignable to type '"before-request"'.             
        Type 'string' is not assignable to type '"before-request"'.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions