const camelToSnakeKeysOfArrayObject = <
T extends { [key: string]: unknown }[]
>(
arr: { [key: string]: unknown }[]
): T =>
arr.map(item =>
Object.entries(item)
.map(([key, value]) => ({
[camelToSnakeCase(key)]: value,
}))
.reduce((acc, item) => ({ ...acc, ...item }), {})
);
Type '{ [x: string]: unknown; }[]' is not assignable to type 'T'.
'{ [x: string]: unknown; }[]' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: unknown; }[]'.
type Alph = 'Q' | 'W' | 'E' | 'R' | 'T' | 'Y' | 'U' | 'I' | 'O' | 'P' | 'A' | 'S' | 'D' | 'F' | 'G' | 'H' | 'J' | 'K' | 'L' | 'X' | 'Z' | 'C' | 'V' | 'B' | 'N' | 'M'
type CamelToSnake<T extends string> = T extends `${infer S1}${Alph}${string}` ? T extends `${S1}${infer S2}` ? `${Lowercase<S1>}_${CamelToSnake<Uncapitalize<S2>>}` : T : T;
type AsdSnake = CamelToSnake<'asdAsdAsd'> // asd_asd_asd
type SnakeToCamel<T extends string> = T extends `${infer S1}_${infer S2}` ? `${Lowercase<S1>}${Capitalize<SnakeToCamel<S2>>}` : T;
type AsdCamel = SnakeToCamel<'asd_asd_asd'> // asdAsdAsd
type SnakePropToCamel<T extends PropertyKey> = T extends string ? SnakeToCamel<T> : T;
type CamelPropToSnake<T extends PropertyKey> = T extends string ? CamelToSnake<T> : T;
let camelToSnakeCase: <T extends PropertyKey>(str: T) => CamelPropToSnake<T>;
type CamelObjectToSnake<T extends {[key: string]: any}> = {
[K in keyof T as CamelPropToSnake<K>]: T[K]
}
function camelCaseObject<T extends {[key: string]: any}>(obj: T) {
return Object.entries(obj)
.reduce((acc, [key, value]) =>
(acc[camelToSnakeCase(key as keyof T)] = value, acc),
{} as CamelObjectToSnake<T>
)
}
function camelToSnakeKeysOfArrayObject<T extends Array<{[key: string]: any}>>(arr: T) {
return arr.map(camelCaseObject) as {
[K in keyof T]: CamelObjectToSnake<T[K]>
};
}
camelToSnakeKeysOfArrayObject([{
aaAa: 1,
bbBb: true
}, {
aaAa: 'ggg'
}]);