Giter Club home page Giter Club logo

Comments (2)

scottpageindysoft avatar scottpageindysoft commented on May 25, 2024 1

Scratch my previous.
I tested it in my module, published the module, and everything worked okay with 2 files.
Beyond that, it spews the events into other types. It was probably doing that with 2 files, but I didn't catch it.

So, you could use what I stated previously, however you will end up with code bleed all over.

I have another solution that I've been using for a few months, but it's hackish.
I'll post it here when I get a minute. Basically you'll need to create your own on and once overrides for the emitter with new generic type defs. From what I recall I also had to wrap another class that extended EventEmitter.

from tiny-typed-emitter.

scottpageindysoft avatar scottpageindysoft commented on May 25, 2024

I finally worked around this issue by following how FeathersJS extends its ServiceTypes interface;
I failed to realize that we can extend interfaces from different files using the module syntax, declare module './declaration-ts-file', within the file of the class you want to extend events.

To make this work, follow these steps:

  1. Create a TS file to store the base events (i.e. ServiceEvents.ts)
  2. In that base events file add your events interface (i.e. export interface ServiceEvents { someEvent: () => void; })
  3. In your base class file, extends TypedEmitter<ServiceEvents> as you normally would
  4. In your child class file, extend the base class
  5. In that same child class file, declare a module with the filename of the base events interface as follows...

declare module './ServiceEvents' { interface ServiceEvents { newEvent: (data: string) => void; } }

You will now have a strongly typed "newEvent" for the child class and observe that other child classes will not have that new event, as expected.

Notice that I created the interfaces and classes in separate files as to eliminate any possibility for a collision with another declare module './ServiceEvents' declaration.

Enjoy.

from tiny-typed-emitter.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.