S
S
SHUPILOID2021-10-06 16:54:26
typescript
SHUPILOID, 2021-10-06 16:54:26

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 }), {})
  );


I want it to accept an array type using generic. But I'm getting an error
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; }[]'.


Please tell me how to implement

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Aetae, 2021-10-06
@SHUPILOID

typescriptlang.org/play

spoiler
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]>
  };
}

Based on the WbICHA variant but supports objects of different signatures, such as:
camelToSnakeKeysOfArrayObject([{
  aaAa: 1,
  bbBb: true
}, {
  aaAa: 'ggg'
}]);

A
Alexandroppolus, 2021-10-06
@Alexandroppolus

Is a template needed here?
Maybe it will be enough

type Data = Array<Record<string, unknown>>;

const camelToSnakeKeysOfArrayObject = (arr: Data): Data => ...

?

W
WbICHA, 2021-10-06
@WblCHA

https://www.typescriptlang.org/play?#code/C4TwDgpg...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question