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:
@@ -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
|
||||
)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Fn } from "hotscript"
|
||||
import { AbstractClass, Simplify } from "type-fest"
|
||||
import { AbstractClass } from "type-fest"
|
||||
|
||||
|
||||
/**
|
||||
@@ -9,10 +8,6 @@ import { AbstractClass, Simplify } from "type-fest"
|
||||
*/
|
||||
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||
|
||||
export interface SimplifyFn extends Fn {
|
||||
return: Simplify<this["arg0"]>
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the static members of a class.
|
||||
* @template Class - A class extending AbstractClass.
|
||||
@@ -20,6 +15,3 @@ export interface SimplifyFn extends Fn {
|
||||
export type StaticMembers<Class extends AbstractClass<any>> = (
|
||||
Omit<Class, "prototype">
|
||||
)
|
||||
export interface StaticMembersFn extends Fn {
|
||||
return: StaticMembers<this["arg0"]>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user