This repository has been archived on 2026-03-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files

99 lines
4.7 KiB
TypeScript

import { Result } from "../result";
import { Type, CustomCommutativeAndType, Either, DefaultIntersect, Comment } from "../type";
import { GetType } from "../get-type";
export declare class MissingKey<T extends Type<any>> {
readonly type: T;
constructor(type: T);
}
export declare class OptionalKey<T extends Type<any>> {
readonly type: T;
constructor(type: T);
}
type WrapperOrType<T> = T extends MissingKey<infer Inner> ? Inner : T extends OptionalKey<infer Inner> ? Inner : T;
type RawDict<V> = {
[key: string]: V;
};
declare abstract class MergeableType<T> extends CustomCommutativeAndType<T> {
and<Incoming>(type: Type<Incoming>): Type<T & Incoming>;
}
export declare class Dict<V> extends MergeableType<RawDict<V>> {
readonly namedKey: string;
readonly valueType: Type<V>;
constructor(v: Type<V>, namedKey?: string);
keyName(key: string): Dict<V>;
check(val: any): Result<RawDict<V>>;
sliceResult(val: any): Result<RawDict<V>>;
}
export declare function dict<V>(v: Type<V>): Dict<V>;
export type FieldDef = Type<any> | MissingKey<any> | OptionalKey<any>;
export type TypeStruct = {
[key: string]: FieldDef;
};
type OptionalPropertyNames<T extends TypeStruct> = {
[K in keyof T]: T[K] extends MissingKey<any> ? K : T[K] extends OptionalKey<any> ? K : never;
}[keyof T];
type UnwrapTypes<T extends TypeStruct> = {
[K in keyof T]: GetType<WrapperOrType<T[K]>>;
};
export type UnwrappedTypeStruct<T extends TypeStruct> = Pick<UnwrapTypes<T>, Exclude<keyof T, OptionalPropertyNames<T>>> & Partial<Pick<UnwrapTypes<T>, OptionalPropertyNames<T>>>;
export type TypeStructFor<T> = {
[K in keyof T]: Type<T[K]>;
};
export type StructFor<T> = Struct<TypeStructFor<T>>;
export declare function keyType<T>(box: MissingKey<Type<T>> | OptionalKey<Type<T>> | Type<T>): Type<T>;
export declare function allowsMissing<T extends Type<any>>(box: MissingKey<T> | T): box is MissingKey<T>;
export declare function allowsOptional<T extends Type<any>>(box: MissingKey<T> | T): box is OptionalKey<T>;
export declare class Struct<T extends TypeStruct> extends MergeableType<UnwrappedTypeStruct<T>> {
readonly definition: T;
readonly exact: boolean;
constructor(definition: T, exact: boolean);
check(val: any): Result<UnwrappedTypeStruct<T>>;
sliceResult(val: any): Result<UnwrappedTypeStruct<T>>;
private checkType;
private checkFields;
}
export declare function subtype<T extends TypeStruct>(def: T): Struct<T>;
export declare function exact<T extends TypeStruct>(def: T): Struct<T>;
export declare function optional<T extends Type<any>>(check: T): OptionalKey<T>;
export declare function allowMissing<T extends Type<any>>(check: T): MissingKey<T>;
type MakeOptional<T extends FieldDef> = T extends Type<any> ? OptionalKey<T> : T extends MissingKey<infer K> ? OptionalKey<K> : T;
type DeepPartialTypeStruct<T extends TypeStruct> = {
[K in keyof T]: T[K] extends Struct<infer T2> ? OptionalKey<Struct<DeepPartialTypeStruct<T2>>> : MakeOptional<T[K]>;
};
type PartialTypeStruct<T extends TypeStruct> = {
[K in keyof T]: MakeOptional<T[K]>;
};
export declare class PartialStruct<T extends TypeStruct> extends MergeableType<UnwrappedTypeStruct<PartialTypeStruct<T>>> {
readonly struct: Struct<T>;
private readonly hiddenStruct;
private readonly hiddenTypeStruct;
constructor(struct: Struct<T>);
check(val: any): Result<UnwrappedTypeStruct<PartialTypeStruct<T>>>;
sliceResult(val: any): Result<UnwrappedTypeStruct<PartialTypeStruct<T>>>;
reify(): Struct<PartialTypeStruct<T>>;
}
export declare function deepPartial<T extends TypeStruct>(ogstruct: Struct<T>): PartialStruct<DeepPartialTypeStruct<T>>;
export declare class MergeIntersect<LVal, RVal, L extends MergeableType<LVal>, R extends MergeableType<RVal>> extends MergeableType<LVal & RVal> {
readonly l: L;
readonly r: R;
protected readonly merged: Type<LVal & RVal>;
constructor(l: L, r: R);
check(val: any): Result<LVal & RVal>;
sliceResult(val: any): Result<LVal & RVal>;
private mergeDictAndMergeable;
private mergeStructAndMergeable;
private mergePartialAndMergeable;
private mergeIntersectAndMergeable;
private mergeInternalAndDict;
private mergeInternalAndStruct;
private mergeInternalAndIntersect;
private mergeInternalAndInternal;
private mergeDicts;
private mergeDictAndStruct;
private mergeStructs;
}
export declare const Nested: readonly [typeof Struct, typeof PartialStruct, typeof Dict, typeof Either, typeof DefaultIntersect, typeof MergeIntersect, typeof Comment];
export type NestedType = InstanceType<(typeof Nested)[number]>;
export declare function partial<T extends TypeStruct>(struct: Struct<T>): PartialStruct<T>;
export {};