Skip to content

Commit

Permalink
fix: modified GetTypeOfConfigKey to resolve to better types
Browse files Browse the repository at this point in the history
Signed-off-by: Logan Nguyen <[email protected]>
  • Loading branch information
quiet-node committed Jan 30, 2025
1 parent 6191603 commit 5b6898e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions packages/config-service/src/services/globalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,30 @@ type StringTypeToActualType<Tstr extends string> = Tstr extends 'string'
? any[]
: never;

// This type maps a configuration key K to its corresponding TypeScript type.
// It first uses ExtractTypeStringFromKey to get the type string and then
// converts that string to the actual TypeScript type using StringTypeToActualType.
// Helper type that determines if a configuration value can be undefined
// based on two conditions: it must be optional (required: false) AND
// have no default value (defaultValue: null).
// Example:
// - GetTypeOfConfigKey<'CHAIN_ID'> would resolve to string.
// - GetTypeOfConfigKey<'BATCH_REQUESTS_ENABLED'> would resolve to boolean.
export type GetTypeOfConfigKey<K extends string> = StringTypeToActualType<ExtractTypeStringFromKey<K>>;
// - For ‘CHAIN_ID’ or ‘OPERATOR_ID_MAIN’ (required: true, defaultValue: null) → false (cannot be undefined as it’s a required config)
// - For ‘WEB_SOCKET_PORT’ (required: false, defaultValue: 8546) → false (cannot be undefined as it has a fallback default value)
// - For ‘WS_CONNECTION_LIMIT_PER_IP’ (required: false, defaultValue: null) → true (can be undefined as it’s not a required config and has no default value)
type CanBeUndefined<K extends string> = K extends keyof typeof _CONFIG
? (typeof _CONFIG)[K]['required'] extends true
? false
: (typeof _CONFIG)[K]['defaultValue'] extends null
? true
: false
: never;

// Type utility that maps configuration keys to their corresponding TypeScript types,
// including undefined for values that can be undefined based on their configuration.
// Example:
// - For 'CHAIN_ID' (required: true, defaultValue: null) -> string
// - For 'WEB_SOCKET_PORT' (required: false, defaultValue: 8546) -> number
// - For 'WS_CONNECTION_LIMIT_PER_IP' (required: false, defaultValue: null) -> number | undefined
export type GetTypeOfConfigKey<K extends string> = CanBeUndefined<K> extends true
? StringTypeToActualType<ExtractTypeStringFromKey<K>> | undefined
: StringTypeToActualType<ExtractTypeStringFromKey<K>>;

// Interface defining the structure of a configuration property.
// Each property includes the environment variable name, its type,
Expand Down
4 changes: 2 additions & 2 deletions packages/config-service/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class ConfigService {
* @param name string
* @returns string | undefined
*/
public static get<K extends ConfigKey>(name: K): GetTypeOfConfigKey<K> | undefined {
return this.getInstance().envs[name] as GetTypeOfConfigKey<K> | undefined;
public static get<K extends ConfigKey>(name: K): GetTypeOfConfigKey<K> {
return this.getInstance().envs[name] as GetTypeOfConfigKey<K>;
}
}

0 comments on commit 5b6898e

Please sign in to comment.