Skip to content

Instantly share code, notes, and snippets.

@cheshirecode
Created January 19, 2022 09:09
Show Gist options
  • Save cheshirecode/550c7fecd3d665db2a975fb24405b09a to your computer and use it in GitHub Desktop.
Save cheshirecode/550c7fecd3d665db2a975fb24405b09a to your computer and use it in GitHub Desktop.
Typescript generic filtering of object keys by prefix/return
// FilterPrefixedKeys<{ a1, b1 }, 'a'> == { a }
export type FilterPrefixedKeys<T, S extends string> = T extends `${S}${infer _X}` ? T : never
// https://stackoverflow.com/questions/69038001/remove-string-from-template-literal-types-in-typescript
// GetPostPrefixSubstring<{ a1, b}, 'a'> == { 1 }
export type GetPostPrefixSubstring<T, S extends string> = T extends `${S}${infer X}` ? X : never
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment