veffect
Version:
powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha
41 lines (40 loc) • 1.73 kB
TypeScript
import { Schema, RefinableSchema, TransformableSchema, DefaultableSchema, NullableSchema } from '../types';
type PropertyKey = string;
type OptionalKey = `${string}?`;
type PropertyKeys<T> = Extract<keyof T, PropertyKey>;
type OptionalPropertyKeys<T> = Extract<keyof T, OptionalKey>;
type InterfaceShape<T> = {
[K in PropertyKeys<T>]: T[K] extends Schema<infer V> ? V : never;
} & {
[K in OptionalPropertyKeys<T> as K extends `${infer Base}?` ? Base : never]?: T[K] extends Schema<infer V> ? V : never;
};
/**
* Interface schema type - adds key optionality capabilities to object schema
*/
export interface InterfaceSchema<T extends Record<string, Schema<any>>> extends Schema<InterfaceShape<T>>, RefinableSchema<InterfaceShape<T>, InterfaceSchema<T>>, TransformableSchema<InterfaceShape<T>, InterfaceSchema<T>>, DefaultableSchema<InterfaceShape<T>, InterfaceSchema<T>>, NullableSchema<InterfaceShape<T>, InterfaceSchema<T>> {
readonly _tag: 'InterfaceSchema';
readonly properties: T;
}
/**
* Create an interface schema with explicit control over key optionality
* Keys with a ? suffix are treated as optional keys (not values)
*
* @example
* ```ts
* // Key optional (property can be omitted)
* const userSchema = interface({
* "name?": string(),
* age: number()
* });
* // type is { name?: string; age: number }
*
* // Value optional (property must exist but can be undefined)
* const configSchema = interface({
* name: string(),
* timeout: number().optional()
* });
* // type is { name: string; timeout: number | undefined }
* ```
*/
export declare function interface_<T extends Record<string, Schema<any>>>(properties: T): InterfaceSchema<T>;
export { interface_ as interface };