Answer the question
In order to leave comments, you need to log in
How to properly implement a generic?
I have a function that accepts an array from an object with camelCase keys as input and converts only the keys to snake_case into the same array.
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; }[]'.
Answer the question
In order to leave comments, you need to log in
typescriptlang.org/play
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'
}]);
Is a template needed here?
Maybe it will be enough
type Data = Array<Record<string, unknown>>;
const camelToSnakeKeysOfArrayObject = (arr: Data): Data => ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question