Created
October 14, 2023 15:27
-
-
Save serifcolakel/5354453c81d98cacd9bd9778d987110f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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