56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
/**
|
|
* 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>
|
|
|
|
export type Extend<T extends readonly object[]> = (
|
|
T extends readonly [
|
|
infer Super,
|
|
infer Self,
|
|
...infer Rest extends readonly object[],
|
|
]
|
|
? Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
|
? Extend<readonly [
|
|
Omit<Super, CommonKeys<Self, Super>> & Self,
|
|
...Rest,
|
|
]>
|
|
: never
|
|
: T extends readonly [infer Self]
|
|
? Self
|
|
: {}
|
|
)
|
|
|
|
export type Override<T extends readonly object[]> = (
|
|
T extends readonly [
|
|
infer Super,
|
|
infer Self,
|
|
...infer Rest extends readonly object[],
|
|
]
|
|
? Override<readonly [
|
|
Omit<Super, CommonKeys<Self, Super>> & Self,
|
|
...Rest,
|
|
]>
|
|
: T extends readonly [infer Self]
|
|
? Self
|
|
: {}
|
|
)
|
|
|
|
/**
|
|
* 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
|
|
)
|