@@ -1,4 +1,4 @@
|
||||
/**
|
||||
* Extracts the common keys between two types
|
||||
*/
|
||||
export type CommonKeys2<A, B> = Extract<keyof A, keyof B>
|
||||
export type CommonKeys<A, B> = Extract<keyof A, keyof B>
|
||||
|
||||
@@ -1,69 +1,54 @@
|
||||
import type { CommonKeys2 } from "./CommonKeys"
|
||||
import type { CommonKeys } from "./CommonKeys"
|
||||
|
||||
|
||||
export type Extend<T extends readonly object[]> = (
|
||||
export type Extend<Super, Self> =
|
||||
Extendable<Super, Self> extends true
|
||||
? Omit<Super, CommonKeys<Self, Super>> & Self
|
||||
: never
|
||||
|
||||
export type Extendable<Super, Self> =
|
||||
Pick<Self, CommonKeys<Self, Super>> extends Pick<Super, CommonKeys<Self, Super>>
|
||||
? true
|
||||
: false
|
||||
|
||||
|
||||
export type ExtendAll<T extends readonly object[]> =
|
||||
T extends readonly [
|
||||
infer Super,
|
||||
infer Self,
|
||||
...infer Rest extends readonly object[],
|
||||
]
|
||||
? Pick<Self, CommonKeys2<Self, Super>> extends Pick<Super, CommonKeys2<Self, Super>>
|
||||
? Extend<readonly [
|
||||
Omit<Super, CommonKeys2<Self, Super>> & Self,
|
||||
...Rest,
|
||||
]>
|
||||
? Extendable<Super, Self> extends true
|
||||
? ExtendAll<readonly [Extend<Super, Self>, ...Rest]>
|
||||
: never
|
||||
: T extends readonly [infer Self]
|
||||
? Self
|
||||
: {}
|
||||
)
|
||||
|
||||
export type Extend2<Self, Super> = Omit<Super, CommonKeys2<Self, Super>> & Self
|
||||
|
||||
|
||||
export type Override<T extends readonly object[]> = (
|
||||
export type ExtendableAll<T extends readonly object[]> =
|
||||
T extends readonly [
|
||||
infer Super,
|
||||
infer Self,
|
||||
...infer Rest extends readonly object[],
|
||||
]
|
||||
? Override<readonly [
|
||||
Omit<Super, CommonKeys2<Self, Super>> & Self,
|
||||
...Rest,
|
||||
]>
|
||||
: 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, CommonKeys2<Self, Super>> extends Pick<Super, CommonKeys2<Self, Super>>
|
||||
? Extendable<readonly [
|
||||
Omit<Super, CommonKeys2<Self, Super>> & Self,
|
||||
...Rest,
|
||||
]>
|
||||
? Extendable<Super, Self> extends true
|
||||
? ExtendableAll<readonly [Extend<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
|
||||
)
|
||||
// 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
|
||||
// )
|
||||
|
||||
15
src/Types/Merge.ts
Normal file
15
src/Types/Merge.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import type { CommonKeys } from "./CommonKeys"
|
||||
|
||||
|
||||
export type Merge<Super, Self> = Omit<Super, CommonKeys<Self, Super>> & Self
|
||||
|
||||
export type MergeAll<T extends readonly object[]> =
|
||||
T extends readonly [
|
||||
infer Super,
|
||||
infer Self,
|
||||
...infer Rest extends readonly object[],
|
||||
]
|
||||
? MergeAll<readonly [Merge<Super, Self>, ...Rest]>
|
||||
: T extends readonly [infer Self]
|
||||
? Self
|
||||
: {}
|
||||
@@ -1,2 +1,4 @@
|
||||
export * from "./CommonKeys"
|
||||
export * from "./Extend"
|
||||
export * from "./Merge"
|
||||
export * from "./StaticType"
|
||||
|
||||
14
src/tests.ts
14
src/tests.ts
@@ -1,7 +1,21 @@
|
||||
import { Schema as S } from "@effect/schema"
|
||||
import { reaction, runInAction } from "mobx"
|
||||
import type { Simplify } from "type-fest"
|
||||
import { Jsonifiable, MutableTaggedClass } from "./Schema"
|
||||
import { ObservableClass } from "./Schema/MobX"
|
||||
import type { ExtendAll } from "./Types"
|
||||
|
||||
|
||||
type TestA = {
|
||||
heugneu: string
|
||||
type: "Type1" | "Type2"
|
||||
}
|
||||
|
||||
type TestB = {
|
||||
type: "Type1"
|
||||
}
|
||||
|
||||
type Merged = Simplify<ExtendAll<[TestA, TestB]>>
|
||||
|
||||
|
||||
class User extends MutableTaggedClass<User>()("User", {
|
||||
|
||||
Reference in New Issue
Block a user