Skip to content

Commit

Permalink
Merge pull request #52 from Dashlane/fix/create-event-bus-return-type
Browse files Browse the repository at this point in the history
Fix/createEventBus return type
  • Loading branch information
bastienGranger authored Jul 28, 2022
2 parents 36cf3eb + 784e764 commit 973d63b
Show file tree
Hide file tree
Showing 6 changed files with 1,167 additions and 1,820 deletions.
15 changes: 15 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { InitialOptionsTsJest } from "ts-jest/dist/types";

const config: InitialOptionsTsJest = {
globals: {
"ts-jest": {
tsconfig: "tsconfig.json",
},
},
moduleFileExtensions: ["ts", "js"],
preset: "ts-jest",
rootDir: "test",
testMatch: ["**/*.spec.ts"],
verbose: true,
};
export default config;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ts-event-bus",
"version": "4.1.0",
"version": "4.1.1",
"description": "Distributed messaging in Typescript",
"main": "build/index.js",
"typings": "build/index.d.ts",
Expand Down Expand Up @@ -45,9 +45,12 @@
"@types/chrome": "^0.0.155",
"@types/jest": "^26.0.20",
"@types/ws": "^3.2.1",
"expect-type": "^0.13.0",
"husky": "^4.3.8",
"jest": "^26.6.3",
"jest": "^28.1.3",
"npm-run-all": "^4.1.5",
"ts-jest": "^28.0.7",
"ts-node": "^10.9.1",
"tslint": "^5.6.0",
"typescript": "~4.1.3",
"typescript-formatter": "^7.2.2",
Expand Down
7 changes: 4 additions & 3 deletions src/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ export function combineEvents<

export function createEventBus<
C extends EventDeclaration,
T extends Array<keyof C>
T extends Array<keyof C> = []
>(args: {
events: C;
channels?: Channel[];
ignoredEvents?: T;
}): Omit<C, T[number]> {
}): T extends [] ? C : Omit<C, T[number]> {

const transports = (args.channels || []).map(
(c) => new Transport(c, (args.ignoredEvents as string[]))
Expand All @@ -62,5 +62,6 @@ export function createEventBus<
}
}

return eventBus as Omit<C, T[number]>
return eventBus as T extends [] ? C : Omit<C, T[number]>
}

2 changes: 1 addition & 1 deletion test/Event.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { slot } from './../src/Slot'
import { combineEvents, createEventBus, omitEvents } from './../src/Events'
import { combineEvents, createEventBus } from './../src/Events'
import { TestChannel } from './TestChannel'
import { DEFAULT_PARAM } from './../src/Constants'

Expand Down
32 changes: 32 additions & 0 deletions test/Types.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { expectTypeOf } from 'expect-type'
import { createEventBus } from '../src/Events'
import { Slot, slot } from '../src/Slot'

describe('createEventBus', () => {
type Events = {
numberToString: Slot<number, string>
eventToIgnore: Slot<void, void>
}

type FilteredEvents = {
numberToString: Slot<number, string>
}

const events: Events = {
numberToString: slot<number, string>(),
eventToIgnore: slot<void, void>(),
}

it('should return the right types regardless of an ingnoredEvents list being passed or not', () => {
const eventBus = createEventBus({ events })
const eventBusWithIgnored = createEventBus({
events,
ignoredEvents: ['eventToIgnore'],
})

expectTypeOf(eventBus).toEqualTypeOf<Events>()
expectTypeOf(eventBus).not.toEqualTypeOf<FilteredEvents>()
expectTypeOf(eventBusWithIgnored).not.toEqualTypeOf<Events>()
expectTypeOf(eventBusWithIgnored).toEqualTypeOf<FilteredEvents>()
})
})
Loading

0 comments on commit 973d63b

Please sign in to comment.