From 3186ce1f4dada891c30acc45d2a4e7bd6b42e4dc Mon Sep 17 00:00:00 2001 From: Rachit Srivastava Date: Wed, 18 Sep 2024 08:29:50 +0530 Subject: [PATCH] Refactor and Optimize Effect Handling This PR refactors the Effect and TradeEffect handling code for better readability and type safety. The changes include: 1.Replacing interface with type where applicable. 2.Defaulting the generic type to unknown instead of any in Effect. 3.Simplifying EffectWithPayload to a type alias. --- src/effects.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/effects.ts b/src/effects.ts index 2c495f6..f16ee41 100644 --- a/src/effects.ts +++ b/src/effects.ts @@ -10,28 +10,32 @@ export interface EffectMeta { seq?: number; } -export interface Effect

{ +export interface Effect

{ readonly type: EffectType; readonly payload: P; readonly meta: EffectMeta; } -export interface EffectWithPayload

extends Effect

{ - payload: P; -} +export type EffectWithPayload

= Effect

; -export function createEffect

(type: EffectType, payload: P, meta: EffectMeta = {}): Effect

{ +export function createEffect

( + type: EffectType, + payload: P, + meta: EffectMeta = {} +): EffectWithPayload

{ return { type, payload, meta }; } export interface TradeEffectPayload { readonly price: string; - readonly size: string; // TODO: Name? + readonly size: string; readonly side: TradeSide; } -export interface TradeEffect extends EffectWithPayload {} +export type TradeEffect = EffectWithPayload; -export const tradeEffect = (payload: TradeEffectPayload) => createEffect(EffectType.Trade, payload); +export const tradeEffect = (payload: TradeEffectPayload): TradeEffect => + createEffect(EffectType.Trade, payload); -export const isTradeEffect = (effect: Effect): effect is TradeEffect => effect.type === EffectType.Trade; +export const isTradeEffect = (effect?: Effect): effect is TradeEffect => + effect?.type === EffectType.Trade;