Skip to content

Instantly share code, notes, and snippets.

@tolotrasmile
Last active May 2, 2023 16:05
Show Gist options
  • Save tolotrasmile/3e0e99701945b68ce606b6e27f55473c to your computer and use it in GitHub Desktop.
Save tolotrasmile/3e0e99701945b68ce606b6e27f55473c to your computer and use it in GitHub Desktop.
// https://github.com/total-typescript/shoehorn
export type NoInfer<T> = [T][T extends any ? 0 : never];
export type DeepPartial<T> = T extends (...args: any[]) => unknown
? T | undefined
: T extends object
? T extends ReadonlyArray<infer ItemType>
? ItemType[] extends T
? readonly ItemType[] extends T
? ReadonlyArray<DeepPartial<ItemType | undefined>>
: Array<DeepPartial<ItemType | undefined>>
: DeepPartialObject<T>
: DeepPartialObject<T>
: T;
export type DeepPartialObject<ObjectType extends object> = {
[KeyType in keyof ObjectType]?: DeepPartial<ObjectType[KeyType]>;
};
export function mock<T>(mock: DeepPartial<NoInfer<T>>): T {
return mock as T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment