Files
traitify-ts/src/util/extend.ts
Julien Valverdé a84f42ee91
All checks were successful
continuous-integration/drone/push Build is passing
Extend fix
2024-02-25 02:41:42 +01:00

72 lines
2.0 KiB
TypeScript

import { CommonKeys } from "."
// type ExtendReducer<Super, Self> = (
// Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
// ? Omit<Super, CommonKeys<Self, Super>> & Self
// : never
// )
// interface ExtendReducerFn extends Fn {
// return: ExtendReducer<this["arg0"], this["arg1"]>
// }
// export type ExtendFn = Tuples.Reduce<ExtendReducerFn, {}>
// export type ExtendableFn = ComposeLeft<[
// ExtendFn,
// Match<[
// Match.With<never, false>,
// Match.With<any, true>,
// ]>
// ]>
// TODO: use OverrideProperties from type-fest?
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 Extendable<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>>
? Extendable<readonly [
Omit<Super, CommonKeys<Self, Super>> & Self,
...Rest,
]>
: false
: true
)
export type NonExtendableKeys<T extends readonly object[]> = (
T extends readonly [
infer Super extends object,
infer Self extends object,
...infer Rest extends readonly object[],
]
? {[K in keyof Super & keyof Self]: Self[K] extends Super[K]
? never
: K
}[keyof Super & keyof Self]
| NonExtendableKeys<readonly [
Super & Self,
...Rest,
]>
: void
)