andywer / typed-emitter

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

Cannot use namespace 'TypedEmitter' as a type. #39

Open coeing opened 1 year ago

coeing commented 1 year ago

Getting this error when importing the library the way it is written in the documentation: Cannot use namespace 'TypedEmitter' as a type.

The code which is working for me looks like this:

import TypedEmitter from "typed-emitter";

...

public events = new EventEmitter() as TypedEmitter.default<SwitchJobEvents>;

Maybe it has something to do with the fact that my application is compiled as ESM?

trevor-leach commented 1 year ago

I have the same problem. I found the following works for me:

import { type EventMap } from "typed-emitter";
type TypedEventEmitter<Events extends EventMap> = import("typed-emitter").default<Events>;

The problem seems to be assigning an interface to the module object via the default export. I think the issue would be fixed with the following:

- interface TypedEventEmitter<Events extends EventMap> {
+ export interface TypedEventEmitter<Events extends EventMap> {

This should conserve backwards compatibility while allowing the following:

import { EventMap , TypedEventEmitter } from "typed-emitter";
nk980113 commented 1 year ago

I have the same issue.

hsorbo commented 1 year ago

Our workaround is:

import TypedEventEmitter, { EventMap } from "typed-emitter";
type TypedEmitter<T extends EventMap> = TypedEventEmitter.default<T>;