Skip to content

Instantly share code, notes, and snippets.

@davlet61
Last active February 22, 2023 13:36
Show Gist options
  • Select an option

  • Save davlet61/61ff6bc39fdcb1c4d02c990b7198d55b to your computer and use it in GitHub Desktop.

Select an option

Save davlet61/61ff6bc39fdcb1c4d02c990b7198d55b to your computer and use it in GitHub Desktop.
Snake to Camel
type SnakeToCamelCase<T> = T extends `id${infer S}_${infer N}`
? `${SnakeToCamelCase<N>}Id${S}`
: T extends `${infer F}_${infer N}`
? `${F}${Capitalize<N>}`
: T;
type MappedType<T> = { [K in keyof T as SnakeToCamelCase<K>] : T[K] };
/*
This is a specific case where some of the snake case types start with `ids`.
The snippet can be easily modified for any use case.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment