This commit is contained in:
40
src/util/extend.ts
Normal file
40
src/util/extend.ts
Normal 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
|
||||
)
|
||||
Reference in New Issue
Block a user