0.1.4 (#4)
All checks were successful
continuous-integration/drone/push Build is passing

Co-authored-by: Julien Valverdé <julien.valverde@mailo.com>
Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
Julien Valverdé
2024-02-20 01:39:38 +01:00
parent c1c3c07524
commit de3b23018a
11 changed files with 535 additions and 362 deletions

View File

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