Skip to content

Instantly share code, notes, and snippets.

@serifcolakel
Created October 14, 2023 15:27
Show Gist options
  • Save serifcolakel/5354453c81d98cacd9bd9778d987110f to your computer and use it in GitHub Desktop.
Save serifcolakel/5354453c81d98cacd9bd9778d987110f to your computer and use it in GitHub Desktop.
const data = {
NaMe: "name",
aGe: 12,
a: "mask",
};
export type TLiteral = "UPPER" | "LOWER" | "CAPITALIZE" | "UNCAPITALIZE";
export type TransformKeys<T, L extends TLiteral> = {
[K in keyof T as L extends "UPPER"
? Uppercase<string & K>
: L extends "LOWER"
? Lowercase<string & K>
: L extends "CAPITALIZE"
? Capitalize<string & K>
: L extends "UNCAPITALIZE"
? Uncapitalize<string & K>
: never]: T[K];
};
type UpperCaseKeys = TransformKeys<typeof data, "CAPITALIZE">; // ? { NaMe: string; aGe: number; a: string; }
type LowerCaseKeys = TransformKeys<typeof data, "LOWER">; // ? { name: string; age: number; a: string; }
type CapitalizeKeys = TransformKeys<typeof data, "UPPER">; // ? { NAME: string; AGE: number; A: string; }
type UncapitalizeKeys = TransformKeys<typeof data, "UNCAPITALIZE">; // ? { nAme: string; aGe: number; a: string; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment