object-shape-tester
Version:
Test object properties and value types.
76 lines (75 loc) • 3.04 kB
TypeScript
import { type AnyFunction, type AnyObject } 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';
import { type IsAny, type IsUnknown } from 'type-fest';
/**
* 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;
/**
* 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;