Extend
All checks were successful
Lint / lint (push) Successful in 12s

This commit is contained in:
Julien Valverdé
2024-07-29 20:32:39 +02:00
parent a07181f758
commit 5690f75cdf
5 changed files with 66 additions and 50 deletions

View File

@@ -1,4 +1,4 @@
/** /**
* Extracts the common keys between two types * 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>

View File

@@ -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 [ T extends readonly [
infer Super, infer Super,
infer Self, infer Self,
...infer Rest extends readonly object[], ...infer Rest extends readonly object[],
] ]
? Pick<Self, CommonKeys2<Self, Super>> extends Pick<Super, CommonKeys2<Self, Super>> ? Extendable<Super, Self> extends true
? Extend<readonly [ ? ExtendAll<readonly [Extend<Super, Self>, ...Rest]>
Omit<Super, CommonKeys2<Self, Super>> & Self,
...Rest,
]>
: never : never
: T extends readonly [infer Self] : T extends readonly [infer Self]
? Self ? Self
: {} : {}
)
export type Extend2<Self, Super> = Omit<Super, CommonKeys2<Self, Super>> & Self export type ExtendableAll<T extends readonly object[]> =
export type Override<T extends readonly object[]> = (
T extends readonly [ T extends readonly [
infer Super, infer Super,
infer Self, infer Self,
...infer Rest extends readonly object[], ...infer Rest extends readonly object[],
] ]
? Override<readonly [ ? Extendable<Super, Self> extends true
Omit<Super, CommonKeys2<Self, Super>> & Self, ? ExtendableAll<readonly [Extend<Super, Self>, ...Rest]>
...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,
]>
: false : false
: true : true
)
export type NonExtendableKeys<T extends readonly object[]> = ( // export type NonExtendableKeys<T extends readonly object[]> = (
T extends readonly [ // T extends readonly [
infer Super extends object, // infer Super extends object,
infer Self extends object, // infer Self extends object,
...infer Rest extends readonly object[], // ...infer Rest extends readonly object[],
] // ]
? {[K in keyof Super & keyof Self]: Self[K] extends Super[K] // ? {[K in keyof Super & keyof Self]: Self[K] extends Super[K]
? never // ? never
: K // : K
}[keyof Super & keyof Self] // }[keyof Super & keyof Self]
| NonExtendableKeys<readonly [ // | NonExtendableKeys<readonly [
Super & Self, // Super & Self,
...Rest, // ...Rest,
]> // ]>
: void // : void
) // )

15
src/Types/Merge.ts Normal file
View 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
: {}

View File

@@ -1,2 +1,4 @@
export * from "./CommonKeys" export * from "./CommonKeys"
export * from "./Extend"
export * from "./Merge"
export * from "./StaticType" export * from "./StaticType"

View File

@@ -1,7 +1,21 @@
import { Schema as S } from "@effect/schema" import { Schema as S } from "@effect/schema"
import { reaction, runInAction } from "mobx" import { reaction, runInAction } from "mobx"
import type { Simplify } from "type-fest"
import { Jsonifiable, MutableTaggedClass } from "./Schema" import { Jsonifiable, MutableTaggedClass } from "./Schema"
import { ObservableClass } from "./Schema/MobX" 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", { class User extends MutableTaggedClass<User>()("User", {