ZodSchemaClassOf extend
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-02-08 02:39:38 +01:00
parent ffce582e03
commit ed3f8fb643
3 changed files with 18 additions and 10 deletions

40
src/util/extend.ts Normal file
View File

@@ -0,0 +1,40 @@
/**
* Represents the common keys between two types.
* @template A - The first type.
* @template B - The second type.
*/
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
/**
* Merges an inheritance tree defined by an array of types, considering overrides.
* @template T - An array of types representing the inheritance tree.
*/
export type Extend<T extends readonly any[]> = (
T extends [infer Super, infer Self, ...infer Rest]
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
? Extend<[
Omit<Super, CommonKeys<Self, Super>> & Self,
...Rest,
]>
: never
: T extends [infer Self]
? Self
: void
)
/**
* Merges an inheritance tree defined by an array of types without allowing overrides.
* @template T - An array of types representing the inheritance tree.
*/
export type ExtendWithoutOverriding<T extends readonly any[]> = (
T extends [infer Super, infer Self, ...infer Rest]
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
? ExtendWithoutOverriding<[
Super & Self,
...Rest,
]>
: never
: T extends [infer Self]
? Self
: void
)