Skip to content

Commit

Permalink
Fix support for typed subscriptions for multiple event names for `.ev…
Browse files Browse the repository at this point in the history
…ents()` (#74)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
airhorns and sindresorhus committed Jan 4, 2021
1 parent 30093fd commit 53caa07
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -237,7 +237,7 @@ declare class Emittery<
```
*/
events<Name extends keyof EventData>(
eventName: Name
eventName: Name | Name[]
): AsyncIterableIterator<EventData[Name]>;

/**
Expand Down
24 changes: 24 additions & 0 deletions index.test-d.ts
Expand Up @@ -188,6 +188,30 @@ type AnyListener = (eventData?: unknown) => void | Promise<void>;
};
}

// Strict typing for `.events` iterator
{
const testEventsIterator = async () => {
const ee = new Emittery<{
value: string;
open: undefined;
close: undefined;
}>();

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

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

const ee2 = new Emittery();
for await (const event of ee2.events('unknown')) {
expectType<any>(event);
}
};
}

// Compatibility with p-event, without explicit types
{
const ee = new Emittery();
Expand Down

0 comments on commit 53caa07

Please sign in to comment.