Last active
October 13, 2023 13:59
-
-
Save barfi/44bfc0b4631247f81b0d32b3bd879f0c to your computer and use it in GitHub Desktop.
TS string literals re-mapping
This file contains hidden or 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
| type UserStruct = { | |
| 'prefix:name': string | |
| 'prefix:age': number | |
| lastName: string | |
| } | |
| type RemovePrefix<T> = T extends `prefix:${infer U}` ? U : T | |
| type RemovePrefixFromStruct<T> = { | |
| [K in keyof T as RemovePrefix<K>]: T[K] | |
| } | |
| const user = { | |
| name: 'John', | |
| age: 25, | |
| lastName: 'Doe' | |
| } satisfies RemovePrefixFromStruct<UserStruct> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment