@xylabs/object
v5.0.87
Published
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Keywords
Readme
@xylabs/object
Base functionality used throughout XY Labs TypeScript/JavaScript libraries
Reference
@xylabs/object
Modules
| Module | Description | | ------ | ------ | | index-deprecated | - | | index-un-deprecated | - | | index | - |
index
index-deprecated
classes
### <a id="IsObjectFactory"></a>IsObjectFactoryFactory class for creating type-guard functions that validate objects against a given shape and optional additional checks.
Type Parameters
| Type Parameter |
| ------ |
| T extends TypedObject |
Constructors
Constructor
new IsObjectFactory<T>(): IsObjectFactory<T>;Returns
IsObjectFactory<T>
Methods
create()
create(shape?: ObjectTypeShape, additionalChecks?: TypeCheck<TypedObject>[]): TypeCheck<T>;Creates a type-guard function that validates an object matches the given shape and passes additional checks.
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| shape? | ObjectTypeShape | An optional map of property names to expected types. |
| additionalChecks? | TypeCheck<TypedObject>[] | Optional extra type-check functions to run after shape validation. |
Returns
TypeCheck<T>
A type-guard function for type T.
### <a id="ObjectWrapper"></a>ObjectWrapperAbstract base class that wraps an object and provides typed access to it.
Extended by
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | EmptyObject |
Constructors
Constructor
new ObjectWrapper<T>(obj: T): ObjectWrapper<T>;Parameters
| Parameter | Type |
| ------ | ------ |
| obj | T |
Returns
ObjectWrapper<T>
Properties
| Property | Modifier | Type |
| ------ | ------ | ------ |
| obj | readonly | T |
Accessors
stringKeyObj
Get Signature
get protected stringKeyObj(): StringKeyObject;Returns
### <a id="ValidatorBase"></a>ValidatorBaseAbstract base class for validators that wraps a partial object and provides a validation method.
Extends
ObjectWrapper<Partial<T>>
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | AnyObject |
Implements
Validator<T>
Constructors
Constructor
new ValidatorBase<T>(obj: T): ValidatorBase<T>;Parameters
| Parameter | Type |
| ------ | ------ |
| obj | T |
Returns
ValidatorBase<T>
Inherited from
Properties
| Property | Modifier | Type | Inherited from |
| ------ | ------ | ------ | ------ |
| obj | readonly | T | ObjectWrapper.obj |
Accessors
stringKeyObj
Get Signature
get protected stringKeyObj(): StringKeyObject;Returns
Inherited from
Methods
validate()
abstract validate(payload: T): Promisable<Error[]>;Parameters
| Parameter | Type |
| ------ | ------ |
| payload | T |
Returns
Promisable<Error[]>
Implementation of
functions
### <a id="createDeepMerge"></a>createDeepMergefunction createDeepMerge(options: MergeOptions): <T>(...objects: T) => MergeAll<T>;Creates a deep merge function with the specified options.
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| options | MergeOptions | Options for merging. |
Returns
A deep merge function configured for the specified options.
<T>(...objects: T): MergeAll<T>;Type Parameters
| Type Parameter |
| ------ |
| T extends AnyObject[] |
Parameters
| Parameter | Type |
| ------ | ------ |
| ...objects | T |
Returns
MergeAll<T>
### <a id="isObject"></a>isObjectCall Signature
function isObject(value: unknown): value is object;Type guard that checks whether a value is a plain object (not null and not an array).
Parameters
| Parameter | Type |
| ------ | ------ |
| value | unknown |
Returns
value is object
Call Signature
function isObject<T>(value: T): value is Extract<T, object>;Type guard that checks whether a value is a plain object (not null and not an array).
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is Extract<T, object>
### <a id="isType"></a>isTypefunction isType(value: unknown, expectedType: FieldType): boolean;Parameters
| Parameter | Type |
| ------ | ------ |
| value | unknown |
| expectedType | FieldType |
Returns
boolean
Deprecated
use from @xylabs/typeof instead
### <a id="omitBy"></a>omitByfunction omitBy<T>(
obj: T,
predicate: OmitByPredicate,
maxDepth?: number): Partial<T>;Creates a new object excluding properties that satisfy the predicate, with optional recursive depth.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| obj | T | undefined | The source object to omit properties from. |
| predicate | OmitByPredicate | undefined | A function that returns true for properties to exclude. |
| maxDepth | number | 1 | Maximum recursion depth for nested objects. |
Returns
Partial<T>
A partial copy of the object without matching properties.
### <a id="omitByPrefix"></a>omitByPrefixfunction omitByPrefix<T, P>(
payload: T,
prefix: P,
maxDepth?: number): DeepOmitStartsWith<T, P>;Omits all properties whose keys start with the given prefix, recursively through nested objects.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
| P extends string |
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| payload | T | undefined | The source object. |
| prefix | P | undefined | The string prefix to match keys against. |
| maxDepth | number | 100 | Maximum recursion depth. |
Returns
DeepOmitStartsWith<T, P>
A new object without properties that have matching prefixed keys.
### <a id="pickBy"></a>pickByfunction pickBy<T>(
obj: T,
predicate: PickByPredicate,
maxDepth?: number): Partial<T>;Creates a new object containing only the properties that satisfy the predicate, with optional recursive depth.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| obj | T | undefined | The source object to pick properties from. |
| predicate | PickByPredicate | undefined | A function that returns true for properties to include. |
| maxDepth | number | 1 | Maximum recursion depth for nested objects. |
Returns
Partial<T>
A partial copy of the object with only matching properties.
### <a id="pickByPrefix"></a>pickByPrefixfunction pickByPrefix<T, P>(
payload: T,
prefix: P,
maxDepth?: number): DeepPickStartsWith<T, P>;Picks all properties whose keys start with the given prefix, recursively through nested objects.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
| P extends string |
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| payload | T | undefined | The source object. |
| prefix | P | undefined | The string prefix to match keys against. |
| maxDepth | number | 100 | Maximum recursion depth. |
Returns
DeepPickStartsWith<T, P>
A new object containing only properties with matching prefixed keys.
### <a id="removeFields"></a>removeFieldsfunction removeFields<T, K>(obj: T, fields: K[]): Omit<T, K>;Returns a shallow copy of the object with the specified fields removed.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
| K extends string | number | symbol |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| obj | T | The source object. |
| fields | K[] | An array of keys to remove. |
Returns
Omit<T, K>
A new object without the specified fields.
### <a id="toSafeJson"></a>toSafeJsonfunction toSafeJson(value: unknown, maxDepth?: number): unknown;Converts a value to a JSON-safe representation, handling circular references and non-serializable types.
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| value | unknown | undefined | The value to convert. |
| maxDepth | number | 3 | Maximum recursion depth. |
Returns
unknown
A JSON-safe value.
### <a id="toSafeJsonArray"></a>toSafeJsonArrayfunction toSafeJsonArray(
value: unknown[],
cycleList?: unknown[],
maxDepth?: number): unknown[];Converts an array to a JSON-safe array, handling circular references and depth limits.
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| value | unknown[] | undefined | The array to convert. |
| cycleList? | unknown[] | undefined | Tracks visited objects to detect circular references. |
| maxDepth? | number | 3 | Maximum recursion depth before truncating. |
Returns
unknown[]
A JSON-safe array representation.
### <a id="toSafeJsonObject"></a>toSafeJsonObjectfunction toSafeJsonObject(
value: object,
cycleList?: unknown[],
maxDepth?: number): JsonObject;Converts an object to a JSON-safe object, handling circular references and depth limits.
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| value | object | undefined | The object to convert. |
| cycleList? | unknown[] | undefined | Tracks visited objects to detect circular references. |
| maxDepth? | number | 3 | Maximum recursion depth before truncating. |
Returns
A JSON-safe object representation.
### <a id="toSafeJsonString"></a>toSafeJsonStringfunction toSafeJsonString(value: unknown, maxDepth?: number): string;Converts a value to a pretty-printed JSON string, safely handling circular references and non-JSON types.
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| value | unknown | undefined | The value to serialize. |
| maxDepth | number | 3 | Maximum recursion depth. |
Returns
string
A formatted JSON string.
### <a id="toSafeJsonValue"></a>toSafeJsonValuefunction toSafeJsonValue(
value: unknown,
cycleList?: unknown[],
maxDepth?: number): unknown;Converts an unknown value to a JSON-safe value, replacing circular references with '[Circular]' and non-JSON types with descriptive placeholder strings.
Parameters
| Parameter | Type | Default value | Description |
| ------ | ------ | ------ | ------ |
| value | unknown | undefined | The value to convert. |
| cycleList? | unknown[] | undefined | Tracks visited objects to detect circular references. |
| maxDepth? | number | 3 | Maximum recursion depth before truncating with '[MaxDepth]'. |
Returns
unknown
A JSON-safe representation of the value.
interfaces
### <a id="ObjectTypeConfig"></a>ObjectTypeConfigConfiguration options for object type checking.
Extends
Properties
| Property | Type | Inherited from |
| ------ | ------ | ------ |
| log? | boolean | Logger | TypeCheckConfig.log |
### <a id="TypeCheckConfig"></a>TypeCheckConfigConfiguration options for type check functions, with optional logging.
Extended by
Properties
| Property | Type |
| ------ | ------ |
| log? | boolean | Logger |
### <a id="TypeCheckOptionalConfig"></a>TypeCheckOptionalConfigType check configuration that marks the value as optional, returning undefined on failure.
Extends
Properties
| Property | Type | Inherited from |
| ------ | ------ | ------ |
| log? | boolean | Logger | TypeCheckConfig.log |
| required | false | - |
### <a id="TypeCheckRequiredConfig"></a>TypeCheckRequiredConfigType check configuration that marks the value as required, causing assertions on failure.
Extends
Properties
| Property | Type | Inherited from |
| ------ | ------ | ------ |
| log? | boolean | Logger | TypeCheckConfig.log |
| required | true | - |
### <a id="Validator"></a>ValidatorInterface for validating objects and returning any errors found.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | AnyObject |
Methods
validate()
validate(payload: T): Promisable<Error[]>;Parameters
| Parameter | Type |
| ------ | ------ |
| payload | T |
Returns
Promisable<Error[]>
type-aliases
### <a id="AnyObject"></a>AnyObjecttype AnyObject = EmptyObject & Partial<Record<TypedKey, unknown>>;Any object, which means that it does not enforce the set of fields that it has. Extending from AnyObject will result in a type that includes the universal set of field names
### <a id="AsOptionalTypeFunction"></a>AsOptionalTypeFunctiontype AsOptionalTypeFunction<T> = <TType>(value: AnyNonPromise) => TType | undefined;A simplified type-narrowing function that returns T or undefined, without assertion support.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends AnyNonPromise | AnyNonPromise |
Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
Returns
TType | undefined
### <a id="AsTypeFunction"></a>AsTypeFunctiontype AsTypeFunction<T> = {
<TType> (value: AnyNonPromise): TType | undefined;
<TType> (value: AnyNonPromise, config: TypeCheckRequiredConfig): TType;
<TType> (value: AnyNonPromise, config:
| TypeCheckConfig
| TypeCheckOptionalConfig): TType | undefined;
<TType> (value: AnyNonPromise, assert: StringOrAlertFunction<TType>): TType | undefined;
<TType> (value: AnyNonPromise, assert: StringOrAlertFunction<TType>, config: TypeCheckRequiredConfig): TType;
<TType> (value: AnyNonPromise, assert: StringOrAlertFunction<TType>, config:
| TypeCheckConfig
| TypeCheckOptionalConfig): TType | undefined;
};A type-narrowing function that attempts to cast a value to T, with optional assertion and configuration overloads.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends AnyNonPromise | AnyNonPromise |
Call Signature
<TType>(value: AnyNonPromise): TType | undefined;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
Returns
TType | undefined
Call Signature
<TType>(value: AnyNonPromise, config: TypeCheckRequiredConfig): TType;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
| config | TypeCheckRequiredConfig |
Returns
TType
Call Signature
<TType>(value: AnyNonPromise, config:
| TypeCheckConfig
| TypeCheckOptionalConfig): TType | undefined;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
| config | | TypeCheckConfig | TypeCheckOptionalConfig |
Returns
TType | undefined
Call Signature
<TType>(value: AnyNonPromise, assert: StringOrAlertFunction<TType>): TType | undefined;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
| assert | StringOrAlertFunction<TType> |
Returns
TType | undefined
Call Signature
<TType>(
value: AnyNonPromise,
assert: StringOrAlertFunction<TType>,
config: TypeCheckRequiredConfig): TType;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
| assert | StringOrAlertFunction<TType> |
| config | TypeCheckRequiredConfig |
Returns
TType
Call Signature
<TType>(
value: AnyNonPromise,
assert: StringOrAlertFunction<TType>,
config:
| TypeCheckConfig
| TypeCheckOptionalConfig): TType | undefined;Type Parameters
| Type Parameter |
| ------ |
| TType extends AnyNonPromise |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | AnyNonPromise |
| assert | StringOrAlertFunction<TType> |
| config | | TypeCheckConfig | TypeCheckOptionalConfig |
Returns
TType | undefined
### <a id="Compare"></a>Comparetype Compare<T> = (a: T, b: T) => number;A comparator function that returns a negative number if a < b, zero if a == b, and a positive number if a > b.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| a | T |
| b | T |
Returns
number
### <a id="DeepOmitStartsWith"></a>DeepOmitStartsWithtype DeepOmitStartsWith<T, Prefix> = T extends infer U[] ? DeepOmitStartsWith<U, Prefix>[] : T extends object ? { [K in keyof T as K extends string ? K extends `${Prefix}${string}` ? never : K : K]: DeepOmitStartsWith<T[K], Prefix> } : T;Recursively omits keys that start with the given prefix, including in nested objects and arrays.
Type Parameters
| Type Parameter |
| ------ |
| T |
| Prefix extends string |
### <a id="DeepPickStartsWith"></a>DeepPickStartsWithtype DeepPickStartsWith<T, Prefix> = T extends infer U[] ? DeepPickStartsWith<U, Prefix>[] : T extends object ? { [K in keyof T as K extends string ? K extends `${Prefix}${string}` ? K : never : K]: DeepPickStartsWith<T[K], Prefix> } : T;Recursively picks only the keys that start with the given prefix, including in nested objects and arrays.
Type Parameters
| Type Parameter |
| ------ |
| T |
| Prefix extends string |
### <a id="DeepRestrictToJson"></a>DeepRestrictToJsontype DeepRestrictToJson<T> = { [K in keyof T as K extends string ? K : never]: T[K] extends (infer U)[] ? DeepRestrictToJson<U>[] : T[K] extends object ? DeepRestrictToJson<T[K]> : T[K] extends JsonValue ? T[K] : never };Recursively restricts an object type to only JSON-compatible values, excluding non-serializable types.
Type Parameters
| Type Parameter |
| ------ |
| T |
### <a id="DeepRestrictToStringKeys"></a>DeepRestrictToStringKeystype DeepRestrictToStringKeys<T> = { [K in keyof T as K extends string ? K : never]: T[K] extends (infer U)[] ? DeepRestrictToStringKeys<U>[] : T[K] extends object ? DeepRestrictToStringKeys<T[K]> : T[K] };Recursively removes all non-string keys from an object type, including in nested objects and arrays.
Type Parameters
| Type Parameter |
| ------ |
| T |
### <a id="EmptyObject"></a>EmptyObjecttype EmptyObject<T> = Exclude<{ [K in keyof T]?: never }, unknown[] | (...args: unknown[]) => unknown | null>;An empty object, which means that it does enforce the set of field names, defaulting to an empty set until extended from, which then adds only those additional fields
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends object | object |
### <a id="JsonArray"></a>JsonArraytype JsonArray = z.infer<typeof JsonArrayZod>;A JSON array containing JSON values.
### <a id="JsonObject"></a>JsonObjecttype JsonObject = z.infer<typeof JsonObjectZod>;A JSON object with string keys and JSON values.
### <a id="JsonValue"></a>JsonValuetype JsonValue = z.infer<typeof JsonValueZod>;A recursive JSON value: string, number, boolean, null, array, or object.
### <a id="OmitByPredicate"></a>OmitByPredicatetype OmitByPredicate<T> = (value: T[keyof T], key: keyof T) => boolean;A predicate function used to determine which properties to omit from an object.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | Record<string, unknown> |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T[keyof T] |
| key | keyof T |
Returns
boolean
### <a id="OmitStartsWith"></a>OmitStartsWithtype OmitStartsWith<T, Prefix> = { [K in keyof T as K extends `${Prefix}${string}` ? never : K]: T[K] };Omits the keys of T that start with the given prefix.
Type Parameters
| Type Parameter |
| ------ |
| T |
| Prefix extends string |
### <a id="Optional"></a>Optionaltype Optional<T, F> = Omit<T, F> & Partial<Pick<T, F>>;Makes the specified fields of T optional while keeping the rest required.
Type Parameters
| Type Parameter |
| ------ |
| T extends object |
| F extends keyof T |
### <a id="Override"></a>Overridetype Override<T1, T2> = Omit<T1, keyof T2> & T2;Overrides properties of T1 with those from T2.
Type Parameters
| Type Parameter |
| ------ |
| T1 |
| T2 |
### <a id="PartialRecord"></a>PartialRecordtype PartialRecord<K, T> = { [P in K]?: T };Type Parameters
| Type Parameter |
| ------ |
| K extends keyof any |
| T |
Deprecated
use Partial<Record<>> instead
### <a id="PickByPredicate"></a>PickByPredicatetype PickByPredicate<T> = (value: T[keyof T], key: keyof T) => boolean;A predicate function used to determine which properties to pick from an object.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | Record<string, unknown> |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T[keyof T] |
| key | keyof T |
Returns
boolean
### <a id="PickStartsWith"></a>PickStartsWithtype PickStartsWith<T, Prefix> = { [K in keyof T as K extends `${Prefix}${string}` ? K : never]: T[K] };Picks only the keys of T that start with the given prefix.
Type Parameters
| Type Parameter |
| ------ |
| T |
| Prefix extends string |
### <a id="Simplify"></a>Simplifytype Simplify<T> = { [K in keyof T]: T[K] } & {
};Flattens an intersection or complex mapped type into a single object type for better readability.
Type Parameters
| Type Parameter |
| ------ |
| T |
### <a id="StringKeyObject"></a>StringKeyObjecttype StringKeyObject<T> = {
[key: string]: T;
};An object type with string keys and values of type T.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T | unknown |
Index Signature
[key: string]: T### <a id="StringOrAlertFunction"></a>StringOrAlertFunctiontype StringOrAlertFunction<T> = string | AssertExMessageFunc<T>;A string message or function that produces an assertion error message for a failed type check.
Type Parameters
| Type Parameter |
| ------ |
| T extends AnyNonPromise |
### <a id="TypeCheck"></a>TypeChecktype TypeCheck<T> = {
(obj: AnyNonPromise): obj is T;
(obj: AnyNonPromise, config: TypeCheckConfig): obj is T;
(obj: AnyNonPromise, config:
| number
| TypeCheckConfig
| undefined): obj is T;
};A type guard function that checks whether a value conforms to type T, with optional configuration.
Type Parameters
| Type Parameter |
| ------ |
| T extends TypedValue |
Call Signature
(obj: AnyNonPromise): obj is T;Parameters
| Parameter | Type |
| ------ | ------ |
| obj | AnyNonPromise |
Returns
obj is T
Call Signature
(obj: AnyNonPromise, config: TypeCheckConfig): obj is T;Parameters
| Parameter | Type |
| ------ | ------ |
| obj | AnyNonPromise |
| config | TypeCheckConfig |
Returns
obj is T
Call Signature
(obj: AnyNonPromise, config:
| number
| TypeCheckConfig
| undefined): obj is T;Parameters
| Parameter | Type |
| ------ | ------ |
| obj | AnyNonPromise |
| config | | number | TypeCheckConfig | undefined |
Returns
obj is T
### <a id="WithAdditional"></a>WithAdditionaltype WithAdditional<T, TAdditional> = TAdditional extends EmptyObject ? T & TAdditional : T;Intersects T with TAdditional if TAdditional is an object, otherwise returns T unchanged.
Type Parameters
| Type Parameter | Default type |
| ------ | ------ |
| T extends EmptyObject | void | - |
| TAdditional extends EmptyObject | void | void |
variables
### <a id="AsObjectFactory"></a>AsObjectFactoryconst AsObjectFactory: {
create: <T>(typeCheck: TypeCheck<T>) => AsTypeFunction<T>;
createOptional: <T>(typeCheck: TypeCheck<T>) => (value: AnyNonPromise) => T | undefined;
};Factory for creating type-narrowing functions for TypedObject types.
Type Declaration
| Name | Type |
| ------ | ------ |
| create() | <T>(typeCheck: TypeCheck<T>) => AsTypeFunction<T> |
| createOptional() | <T>(typeCheck: TypeCheck<T>) => (value: AnyNonPromise) => T | undefined |
### <a id="AsTypeFactory"></a>AsTypeFactoryconst AsTypeFactory: {
create: <T>(typeCheck: TypeCheck<T>) => AsTypeFunction<T>;
createOptional: <T>(typeCheck: TypeCheck<T>) => (value: AnyNonPromise) => T | undefined;
};Factory for creating type-narrowing 'as' functions that cast a value to T or return undefined. Supports optional assertion messages and configuration for required/optional behavior.
Type Declaration
| Name | Type |
| ------ | ------ |
| create() | <T>(typeCheck: TypeCheck<T>) => AsTypeFunction<T> |
| createOptional() | <T>(typeCheck: TypeCheck<T>) => (value: AnyNonPromise) => T | undefined |
### <a id="JsonObjectZod"></a>JsonObjectZodconst JsonObjectZod: ZodRecord<ZodString, ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>>>;Zod schema for a JSON object with string keys and recursive JSON values.
### <a id="asAnyObject"></a>asAnyObjectconst asAnyObject: AsTypeFunction<AnyObject>;Type-narrowing function that casts a value to AnyObject if it is a plain object, or returns undefined.
### <a id="asJsonArray"></a>asJsonArrayconst asJsonArray: {
<T> (value: T): T & unknown[] | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & unknown[];
};Casts a value to JsonArray or returns undefined if it does not conform.
Call Signature
<T>(value: T): T & unknown[] | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & unknown[] | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T & unknown[];Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & unknown[]
### <a id="asJsonObject"></a>asJsonObjectconst asJsonObject: {
<T> (value: T): T & Record<string, unknown> | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & Record<string, unknown>;
};Casts a value to JsonObject or returns undefined if it does not conform.
Call Signature
<T>(value: T): T & Record<string, unknown> | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & Record<string, unknown> | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T & Record<string, unknown>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & Record<string, unknown>
### <a id="asJsonValue"></a>asJsonValueconst asJsonValue: {
<T> (value: T): T | undefined;
<T> (value: T, assert: ZodFactoryConfig): T;
};Casts a value to JsonValue or returns undefined if it does not conform.
Call Signature
<T>(value: T): T | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T
### <a id="deepMerge"></a>deepMergeconst deepMerge: <T>(...objects: T) => MergeAll<T>;Deeply merges multiple objects into a new object.
Type Parameters
| Type Parameter |
| ------ |
| T extends AnyObject[] |
Parameters
| Parameter | Type | Description |
| ------ | ------ | ------ |
| ...objects | T | Multiple objects to merge deeply. The function merges properties from all objects into a new object. If a property exists in multiple objects, the last object's value will be used. If a property is an object, it will be merged recursively. If a property is an array, it will be overwritten by the last object's value. If a property is a primitive value, it will be overwritten by the last object's value. If a property is undefined in the source, it will be skipped. If a property is a symbol, it will be merged as well. |
Returns
MergeAll<T>
A new object with the merged properties.
### <a id="isJsonArray"></a>isJsonArrayconst isJsonArray: <T>(value: T) => value is T & unknown[];Type guard that checks if a value is a valid JSON array.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is T & unknown[]
### <a id="isJsonObject"></a>isJsonObjectconst isJsonObject: <T>(value: T) => value is T & Record<string, unknown>;Type guard that checks if a value is a valid JSON object.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is T & Record<string, unknown>
### <a id="isJsonValue"></a>isJsonValueconst isJsonValue: <T>(value: T) => value is T;Type guard that checks if a value is a valid JSON value.
Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
value is T
### <a id="toJsonArray"></a>toJsonArrayconst toJsonArray: {
<T> (value: T): T & unknown[] | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & unknown[];
};Parses a value into a JsonArray, throwing if it does not conform.
Call Signature
<T>(value: T): T & unknown[] | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & unknown[] | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T & unknown[];Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & unknown[]
### <a id="toJsonObject"></a>toJsonObjectconst toJsonObject: {
<T> (value: T): T & Record<string, unknown> | undefined;
<T> (value: T, assert: ZodFactoryConfig): T & Record<string, unknown>;
};Parses a value into a JsonObject, throwing if it does not conform.
Call Signature
<T>(value: T): T & Record<string, unknown> | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T & Record<string, unknown> | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T & Record<string, unknown>;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T & Record<string, unknown>
### <a id="toJsonValue"></a>toJsonValueconst toJsonValue: {
<T> (value: T): T | undefined;
<T> (value: T, assert: ZodFactoryConfig): T;
};Parses a value into a JsonValue, throwing if it does not conform.
Call Signature
<T>(value: T): T | undefined;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
Returns
T | undefined
Call Signature
<T>(value: T, assert: ZodFactoryConfig): T;Type Parameters
| Type Parameter |
| ------ |
| T |
Parameters
| Parameter | Type |
| ------ | ------ |
| value | T |
| assert | ZodFactoryConfig |
Returns
T
index-un-deprecated
Part of sdk-js
Maintainers
License
See the LICENSE file for license details
