From 2545262fc2b1452cc125c1f4f15d9f6853085298 Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Fri, 17 Feb 2023 16:04:29 +0530 Subject: [PATCH] chore: improve DispatchingStore typings for dispatch streams --- .../src/newstore/DispatchingStore.ts | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/packages/hoppscotch-common/src/newstore/DispatchingStore.ts b/packages/hoppscotch-common/src/newstore/DispatchingStore.ts index c6c71d788..a54b692c8 100644 --- a/packages/hoppscotch-common/src/newstore/DispatchingStore.ts +++ b/packages/hoppscotch-common/src/newstore/DispatchingStore.ts @@ -23,12 +23,13 @@ export const defineDispatchers = < type Dispatch< StoreType, - DispatchersType extends { [x: string]: DispatcherFunc }, - Dispatcher extends keyof DispatchersType + DispatchersType extends { [x: string]: DispatcherFunc } > = { - dispatcher: Dispatcher - payload: Parameters[1] -} + [Dispatcher in keyof DispatchersType]: { + dispatcher: Dispatcher + payload: Parameters[1] + } +}[keyof DispatchersType] export default class DispatchingStore< StoreType, @@ -36,9 +37,7 @@ export default class DispatchingStore< > { #state$: BehaviorSubject #dispatchers: DispatchersType - #dispatches$: Subject< - Dispatch - > = new Subject() + #dispatches$: Subject> = new Subject() constructor(initialValue: StoreType, dispatchers: DispatchersType) { this.#state$ = new BehaviorSubject(initialValue) @@ -70,10 +69,7 @@ export default class DispatchingStore< return this.#dispatches$ } - dispatch({ - dispatcher, - payload, - }: Dispatch) { + dispatch({ dispatcher, payload }: Dispatch) { if (!this.#dispatchers[dispatcher]) throw new Error(`Undefined dispatch type '${String(dispatcher)}'`)