UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

21 lines (20 loc) 1.12 kB
import { NonPlainObject } from "../_internal/NonPlainObject.mjs"; //#region src/types/ToConstantCaseKeys.d.ts type ConstantCase<S extends string> = S extends Lowercase<S> ? Uppercase<S> : S extends `${infer P1}${infer P2}` ? P2 extends Uncapitalize<P2> ? `${Uppercase<P1>}${ConstantCase<P2>}` : `${Uppercase<P1>}_${ConstantCase<Uncapitalize<P2>>}` : Uppercase<S>; /** * Converts the keys of `T` to CONSTANT_CASE recursively. * * This is the return type of the `toConstantCaseKeys` function. Recurses into * plain objects and arrays; built-in objects like `Date`, `Map`, and functions * pass through unchanged. * * @template T - The type whose keys are converted. * * @example * type Response = { userId: number; firstName: string }; * type Converted = ToConstantCaseKeys<Response>; * // => { USER_ID: number; FIRST_NAME: string } */ type ToConstantCaseKeys<T> = T extends NonPlainObject ? T : T extends any[] ? Array<ToConstantCaseKeys<T[number]>> : T extends Record<string, any> ? { [K in keyof T as ConstantCase<string & K>]: ToConstantCaseKeys<T[K]> } : T; //#endregion export { ToConstantCaseKeys };