Skip to content

Instantly share code, notes, and snippets.

@barfi
Last active October 13, 2023 13:59
Show Gist options
  • Save barfi/44bfc0b4631247f81b0d32b3bd879f0c to your computer and use it in GitHub Desktop.
Save barfi/44bfc0b4631247f81b0d32b3bd879f0c to your computer and use it in GitHub Desktop.
TS string literals re-mapping
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