Created
May 31, 2022 17:17
-
-
Save IPRIT/29039791ca3e86230d893dbc0ea65d67 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
interface ObjectConstructor { | |
keys<T>(o: T): ObjectKeys<T> | |
} | |
type ObjectKeys<T> = | |
T extends object ? (keyof T)[] : | |
T extends number ? [] : | |
T extends Array<any> | string ? string[] : | |
never; | |
const tn = <const>{ | |
color: { | |
blue: 'blue', | |
red: 'red' | |
}, | |
size: { | |
small: 'small' | |
} | |
}; | |
type TransformedProps<N extends string, T extends Record<string, any>> = T extends { [P in keyof T]: T[P] } | |
? { [P in keyof T]: `${N}=${T[P]}` } | |
: never; | |
type Transformed<T extends Record<string, any>> = T extends { [P in keyof T]: T[P] } | |
? { [P in keyof T]: TransformedProps<P & string, T[P]> } | |
: never; | |
function transform<T extends Record<string, Record<string, string>>> (params: T) { | |
const props = {} as any; | |
Object.keys(params).forEach(tnKey => { | |
const tnValue = params[tnKey]; | |
const values = {} as any; | |
Object.keys(tnValue).forEach(value => { | |
values[value] = `${tnKey}=${value}`; | |
}); | |
props[tnKey] = values; | |
}); | |
return props as Transformed<T>; | |
} | |
const result = transform(tn); | |
console.log(result.color.blue); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment