Skip to content
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

fix(types): add array parameter types #82

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ declare class Emittery<
```
*/
on<Name extends keyof AllEventData>(
eventName: Name,
eventName: Name | Name[],
listener: (eventData: AllEventData[Name]) => void | Promise<void>
): Emittery.UnsubscribeFn;

Expand Down Expand Up @@ -264,7 +264,7 @@ declare class Emittery<
```
*/
off<Name extends keyof AllEventData>(
eventName: Name,
eventName: Name | Name[],
listener: (eventData: AllEventData[Name]) => void | Promise<void>
): void;

Expand Down Expand Up @@ -292,7 +292,9 @@ declare class Emittery<
emitter.emit('🐶', '🍖'); // Nothing happens
```
*/
once<Name extends keyof AllEventData>(eventName: Name): Promise<AllEventData[Name]>;
once<Name extends keyof AllEventData>(
eventName: Name | Name[]
): Promise<AllEventData[Name]>;

/**
Trigger an event asynchronously, optionally with some data. Listeners are called in the order they were added, but executed concurrently.
Expand Down Expand Up @@ -383,12 +385,12 @@ declare class Emittery<

If `eventName` is given, only the listeners for that event are cleared.
*/
clearListeners(eventName?: keyof EventData): void;
clearListeners<Name extends keyof EventData>(eventName?: Name | Name[]): void;

/**
The number of listeners for the `eventName` or all events if not specified.
*/
listenerCount(eventName?: keyof EventData): number;
listenerCount<Name extends keyof EventData>(eventName?: Name | Name[]): number;

/**
Bind the given `methodNames`, or all `Emittery` methods if `methodNames` is not defined, into the `target` object.
Expand Down
11 changes: 9 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
value: string;
open: undefined;
close: undefined;
other: number;
}>();
ee.on('open', () => {});
ee.on('open', argument => {
Expand All @@ -103,13 +104,18 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
ee.on('value', argument => {
expectType<string>(argument);
});
ee.on(['value', 'other'], argument => {
expectType<string | number>(argument);
});

const listener = (value: string) => undefined;
ee.on('value', listener);
ee.off('value', listener);
const test = async () => {
const event = await ee.once('value');
expectType<string>(event);
const firstEvent = await ee.once(['value', 'other']);
expectType<string | number>(firstEvent);
};

expectError(ee.on('value', (value: number) => {}));
Expand Down Expand Up @@ -195,14 +201,15 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
value: string;
open: undefined;
close: undefined;
other: number;
}>();

for await (const event of ee.events('value')) {
expectType<string>(event);
}

for await (const event of ee.events(['value', 'open'])) {
expectType<string | undefined>(event);
for await (const event of ee.events(['value', 'other'])) {
expectType<string | number>(event);
}

const ee2 = new Emittery();
Expand Down