UNPKG

object-shape-tester

Version:
87 lines (86 loc) 3.37 kB
import { type AnyFunction, type AnyObject, type IsAny, type IsUnknown } from '@augment-vir/common'; import { type Static, type TArray, type TBigInt, type TBoolean, type TNull, type TNumber, type TObject, type TSchema, type TString, type TSymbol, type TUndefined, type TUnsafe } from '@sinclair/typebox'; import { type TypeCheck } from '@sinclair/typebox/compiler'; /** * Output of {@link defineShape}. * * @category Internal */ export type Shape<Init = any> = { default: ShapeInitDefault<Init>; $_schema: ShapeInitSchema<Init>; $_schemaNoExtraKeys: ShapeInitSchema<Init>; $_schemaExtraKeys: ShapeInitSchema<Init>; $_compiledSchema: TypeCheck<any>; $_compiledSchemaNoExtraKeys: TypeCheck<any>; $_compiledSchemaExtraKeys: TypeCheck<any>; runtimeType: ShapeInitType<Init>; }; /** * A special key string which is used to tag {@link Shape} instances so that we know they're shapes * instead of part of the shape itself. * * We don't use `instanceof` with a Class constructor for this because that breaks when you have * multiple versions of object-shape-tester defining and consuming shapes. * * @category Internal */ export declare const shapeIdentifier: unique symbol; /** * A type for defining a shape definition with a direct type definition. * * @category Internal */ export type UnsafeShape<T> = Shape<TUnsafe<T>>; /** * A helper for defining a shape while also immediately providing the type for that shape's values. * * @category Internal */ export declare function unsafeShape<T>(init: any): UnsafeShape<T>; /** * Defines a shape from the given init. * * @category Define */ export declare function defineShape<Init = any>(init: Init): Shape<Init>; /** * Checks if `input` is a Shape. * * @category Internal */ export declare function isShape(input: unknown): input is Shape; /** * Checks if `input` is a TSchema. * * @category Internal */ export declare function isSchema(input: unknown): input is TSchema; /** * Converts the shape init to a TSchema. * * @category Internal */ export declare function shapeInitToSchema(init: unknown): TSchema; /** * Converts a shape init into its runtime type. * * @category Internal */ export type ShapeInitType<Init> = IsAny<Init> extends true ? any : IsUnknown<Init> extends true ? any : Init extends Shape ? Init['runtimeType'] : Static<ShapeInitSchema<Init>>; /** * Converts a shape init into its default value type. * * @category Internal */ export type ShapeInitDefault<Init> = IsAny<Init> extends true ? any : IsUnknown<Init> extends true ? any : Readonly<Static<ShapeInitSchema<Init>>>; /** * Converts a shape init into its schema type. * * @category Internal */ export type ShapeInitSchema<Init> = IsAny<Init> extends true ? any : IsUnknown<Init> extends true ? any : Init extends TSchema ? Init : Init extends { $_schema: infer InnerSchema extends TSchema; } ? InnerSchema : Init extends AnyFunction ? TUnsafe<Init> : Init extends string ? TString : Init extends number ? TNumber : Init extends boolean ? TBoolean : Init extends symbol ? TSymbol : Init extends null ? TNull : Init extends undefined ? TUndefined : Init extends bigint ? TBigInt : Init extends ReadonlyArray<infer InnerShape> ? TArray<ShapeInitSchema<InnerShape>> : Init extends AnyObject ? TObject<{ [Key in keyof Init]: ShapeInitSchema<Init[Key]>; }> : never;