From 5943ffc9e4797a3df9453e83593b79f3c2074931 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Fri, 11 Jul 2025 09:44:25 +0200 Subject: [PATCH] ref: Avoid `enum` usage Apart of directly vendored code, we should avoid `enum`, as it has bundle size impact. --- .../featureFlags/openfeature/types.ts | 21 +++++++------ packages/core/src/utils/syncpromise.ts | 31 +++++++++---------- packages/react/src/types.ts | 6 +--- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/packages/browser/src/integrations/featureFlags/openfeature/types.ts b/packages/browser/src/integrations/featureFlags/openfeature/types.ts index 835e684d86eb..4d16e81489fb 100644 --- a/packages/browser/src/integrations/featureFlags/openfeature/types.ts +++ b/packages/browser/src/integrations/featureFlags/openfeature/types.ts @@ -17,16 +17,17 @@ export const StandardResolutionReasons = { STALE: 'STALE', ERROR: 'ERROR', } as const; -export enum ErrorCode { - PROVIDER_NOT_READY = 'PROVIDER_NOT_READY', - PROVIDER_FATAL = 'PROVIDER_FATAL', - FLAG_NOT_FOUND = 'FLAG_NOT_FOUND', - PARSE_ERROR = 'PARSE_ERROR', - TYPE_MISMATCH = 'TYPE_MISMATCH', - TARGETING_KEY_MISSING = 'TARGETING_KEY_MISSING', - INVALID_CONTEXT = 'INVALID_CONTEXT', - GENERAL = 'GENERAL', -} + +type ErrorCode = + | 'PROVIDER_NOT_READY' + | 'PROVIDER_FATAL' + | 'FLAG_NOT_FOUND' + | 'PARSE_ERROR' + | 'TYPE_MISMATCH' + | 'TARGETING_KEY_MISSING' + | 'INVALID_CONTEXT' + | 'GENERAL'; + export interface Logger { error(...args: unknown[]): void; warn(...args: unknown[]): void; diff --git a/packages/core/src/utils/syncpromise.ts b/packages/core/src/utils/syncpromise.ts index 95aa45598727..0b885910fee3 100644 --- a/packages/core/src/utils/syncpromise.ts +++ b/packages/core/src/utils/syncpromise.ts @@ -2,14 +2,11 @@ import { isThenable } from './is'; /** SyncPromise internal states */ -const enum States { - /** Pending */ - PENDING = 0, - /** Resolved / OK */ - RESOLVED = 1, - /** Rejected / Error */ - REJECTED = 2, -} +const STATE_PENDING = 0; +const STATE_RESOLVED = 1; +const STATE_REJECTED = 2; + +type State = typeof STATE_PENDING | typeof STATE_RESOLVED | typeof STATE_REJECTED; // Overloads so we can call resolvedSyncPromise without arguments and generic argument export function resolvedSyncPromise(): PromiseLike; @@ -46,12 +43,12 @@ type Executor = (resolve: (value?: T | PromiseLike | null) => void, reject * but is not async internally */ export class SyncPromise implements PromiseLike { - private _state: States; + private _state: State; private _handlers: Array<[boolean, (value: T) => void, (reason: any) => any]>; private _value: any; public constructor(executor: Executor) { - this._state = States.PENDING; + this._state = STATE_PENDING; this._handlers = []; this._runExecutor(executor); @@ -135,7 +132,7 @@ export class SyncPromise implements PromiseLike { /** Excute the resolve/reject handlers. */ private _executeHandlers(): void { - if (this._state === States.PENDING) { + if (this._state === STATE_PENDING) { return; } @@ -147,11 +144,11 @@ export class SyncPromise implements PromiseLike { return; } - if (this._state === States.RESOLVED) { + if (this._state === STATE_RESOLVED) { handler[1](this._value as unknown as any); } - if (this._state === States.REJECTED) { + if (this._state === STATE_REJECTED) { handler[2](this._value); } @@ -161,8 +158,8 @@ export class SyncPromise implements PromiseLike { /** Run the executor for the SyncPromise. */ private _runExecutor(executor: Executor): void { - const setResult = (state: States, value?: T | PromiseLike | any): void => { - if (this._state !== States.PENDING) { + const setResult = (state: State, value?: T | PromiseLike | any): void => { + if (this._state !== STATE_PENDING) { return; } @@ -178,11 +175,11 @@ export class SyncPromise implements PromiseLike { }; const resolve = (value: unknown): void => { - setResult(States.RESOLVED, value); + setResult(STATE_RESOLVED, value); }; const reject = (reason: unknown): void => { - setResult(States.REJECTED, reason); + setResult(STATE_REJECTED, reason); }; try { diff --git a/packages/react/src/types.ts b/packages/react/src/types.ts index b29a2dbd1cad..19aacffc5ac3 100644 --- a/packages/react/src/types.ts +++ b/packages/react/src/types.ts @@ -197,11 +197,7 @@ export type Navigation = NavigationStates[keyof NavigationStates]; export type RouteData = any; export type Fetcher = any; -export declare enum HistoryAction { - Pop = 'POP', - Push = 'PUSH', - Replace = 'REPLACE', -} +type HistoryAction = 'POP' | 'PUSH' | 'REPLACE'; export interface RouterState { historyAction: Action | HistoryAction | any;