Comments (2)
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.
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:
- Create a TS file to store the base events (i.e.
ServiceEvents.ts
) - In that base events file add your events interface (i.e.
export interface ServiceEvents { someEvent: () => void; }
) - In your base class file,
extends TypedEmitter<ServiceEvents>
as you normally would - In your child class file, extend the base class
- 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)
- How would I specify a function as one of the items emitted HOT 1
- [Question] Trying to expose specific methods only HOT 2
- Can we use the emit method with a variable for the event name instead of string? HOT 3
- Cannot find module 'tiny-typed-emitter' or its corresponding type declarations.ts(2307) HOT 2
- adding typed events to an extended class that extends EventEmitter? HOT 3
- Keep default any event type
- Provide also raw interface definition instead of just class
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tiny-typed-emitter.