@croct/plug
Version:
A fully-featured devkit for building natively personalized applications.
43 lines (40 loc) • 2.13 kB
text/typescript
import { JsonObject } from '@croct/json';
import { VersionedId, Versioned, Version, CanonicalVersionId } from './versioning.cjs';
import { ComponentVersionId, ComponentContent } from './component.cjs';
interface SlotMap {
}
type LatestSlotVersionMap = {
[K in keyof SlotMap]: {
latest: SlotMap[K];
};
};
interface VersionedSlotMap extends LatestSlotVersionMap {
}
/**
* Creates an intersection of the given types distributing over unions.
*
* The difference between this type and the built-in `&` operator is that the
* `&` operator creates an intersection of the union members instead of
* creating a union of the intersection members. For example, given the types
* `Left = A | B` and `Right = C`, the type `Left & Right` expands to
* `(A | B) & C`, but `Merge<Left, Right>` expands to `A & C | B & C`,
* which improves type inference when narrowing the type.
*/
type Intersection<T, E> = T extends infer O ? O & E : never;
type UnionContent<T = null> = {
[K in ComponentVersionId]: Intersection<ComponentContent<K>, {
_component: K | T;
}>;
};
type UnknownContent = UnionContent[ComponentVersionId] extends never ? (JsonObject & {
_component: string | null;
}) : UnionContent[ComponentVersionId];
type VersionedContent<I extends VersionedSlotId> = Versioned<I, VersionedSlotMap, UnknownContent>;
type DynamicSlotId = any;
type SlotId = keyof VersionedSlotMap extends never ? string : keyof VersionedSlotMap;
type SlotVersion<I extends SlotId> = Version<VersionedSlotMap, I>;
type SlotVersionId<I extends SlotId = SlotId> = CanonicalVersionId<I, VersionedSlotMap>;
type VersionedSlotId<I extends SlotId = SlotId> = VersionedId<I, VersionedSlotMap>;
type CompatibleSlotContent<T extends ComponentVersionId = ComponentVersionId> = UnionContent<never>[T];
type SlotContent<I extends VersionedSlotId = VersionedSlotId, C extends JsonObject = JsonObject> = JsonObject extends C ? (string extends I ? UnknownContent : VersionedContent<I>) : C;
export type { CompatibleSlotContent, DynamicSlotId, SlotContent, SlotId, SlotMap, SlotVersion, SlotVersionId, VersionedSlotId, VersionedSlotMap };