Skip to content

Instantly share code, notes, and snippets.

@ignavan39
Last active March 24, 2023 07:26
Show Gist options
  • Save ignavan39/3cb9afe7052c5162e1249e60faf8ae00 to your computer and use it in GitHub Desktop.
Save ignavan39/3cb9afe7052c5162e1249e60faf8ae00 to your computer and use it in GitHub Desktop.
typescript helpers
export type UniqueArray<T> = T extends readonly [infer X, ...infer Rest]
? InArray<Rest, X> extends true
? ['Encountered value with duplicates:', X]
: readonly [X, ...UniqueArray<Rest>] | readonly [...UniqueArray<Rest>]
: T;
type InArray<T, X> = T extends readonly [X, ...infer _Rest]
? true
: T extends readonly [X]
? true
: T extends readonly [infer _, ...infer Rest]
? InArray<Rest, X>
: false;
export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> &
{
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];
type SnakeToCamelCase<S extends string> = S extends `${infer T}_${infer U}`
? `${T}${Capitalize<SnakeToCamelCase<U>>}`
: S;
export type SnakeToCamelCaseNested<T> = T extends object
? {
[K in keyof T as SnakeToCamelCase<K & string>]: SnakeToCamelCaseNested<T[K]>;
}
: T;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment