Skip to content

Instantly share code, notes, and snippets.

@Arkamis
Created February 20, 2023 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arkamis/6ca6ba1d3088843b0f682b4c73464b48 to your computer and use it in GitHub Desktop.
Save Arkamis/6ca6ba1d3088843b0f682b4c73464b48 to your computer and use it in GitHub Desktop.
Add prefix key to an object but excluding some parameters
type PrefixKey<T, Prefix extends string> = {
[K in keyof T as `${Prefix}_${string & K}`]: T[K]
}
type OmitPrefixed<
T,
Prefix extends string,
Keys extends Extract<keyof T, string>
> = {
[K in keyof T as K extends Keys ? never : `${Prefix}_${string & K}`]: T[K]
}
type AddPrefixExcept<
T,
Prefix extends string,
Keys extends Extract<keyof T, string>
> = OmitPrefixed<T, Prefix, Keys> & {
[K in Keys]: T[K] extends string ? `${Prefix}_${T[K]}` : T[K]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment