@aurbi/ts-binding
Version:
bidirectionally bind serialized & simplified objects to full-featured runtime objects. kinda like a subset of zod, but it goes both ways.
58 lines (57 loc) • 3.01 kB
TypeScript
import { Bound, Literal, SerializationConfig } from "./types";
export * from "./serialization";
export { Bound, Stack } from "./types";
/** represents any type. use with care. */
export declare function any(): Bound<any, any>;
/** validate a value with given functions upon transformation/restoration */
export declare function validated<T extends Literal>(validator: (v: any) => boolean, reason?: (v: any) => string): Bound<T, Literal>;
/** Make a provided type expression optional */
export declare function optional<T, L>(schema: Bound<T, L>): Bound<T | undefined, L | undefined>;
/** Extend an existing object schema with another */
export declare function extendObject<BS extends {}, BD, WS extends {}, WD>(baseSchema: Bound<BS, BD>, withSchema: Bound<WS, WD>): Bound<BS & WS, BD & WD>;
/** expresses a literal value of any type */
export declare function literal<T extends Literal>(value: T): Bound<T, T>;
/** expresses a string value */
export declare const string: () => Bound<string, string>;
/** expresses a numeric value */
export declare const number: () => Bound<number, number>;
/** expresses a boolean value */
export declare const boolean: () => Bound<boolean, boolean>;
/** expresses a null value (called nil, because null is a reserved keyword) */
export declare const nil: () => Bound<null, null>;
/**
* Express an object with unknown key values and associated values
* @param keySchema expression of key type
* @param valueSchema expression of value type
*/
export declare function record<K extends string, V, R extends Record<K, V>>(keySchema: Bound<K>, valueSchema: Bound<V>): Bound<R, Literal>;
/**
* Expresses an array of items all typed-alike.
* For arrays containing multiple types, you'll want to use `union` within this.
* @param itemSchema expression of array element
*/
export declare function array<T>(itemSchema: Bound<T>): Bound<Array<T>, Literal>;
/**
* Expresses an object with a known structure.
* If you're looking for an object with unknown keys, use `record`.
* @param schemaObject Object prototype expressing the structure and expressed types therewithin
*/
export declare function object<T, O extends {
[key: string]: Bound<any, any>;
} = {
[key: string]: Bound<T, Literal>;
}>(schemaObject: {
[K in keyof O as K extends keyof T ? K : never]: O[K];
}): Bound<T, Literal>;
/**
* Expresses a discriminated union.
* @param discriminators validation functions that either return the selected type expression or false
*/
export declare function union<TUnion>(...discriminators: Array<(value: TUnion) => Bound<any, any> | false>): Bound<TUnion, Literal>;
/**
* Express a serialized string that matches a specific schema.
* Unpacked into an object of matching type during transformation,
* and re-packed into a serialized string during restoration.
* @param schema expression of serialized type
*/
export declare function document<T, S = string>(schema: Bound<T>, { serializer, deserializer }?: SerializationConfig<Literal, S>): Bound<T, S>;