Skip to main content
Module

x/unknownutil/is.ts>isObjectOf

🦕 A lightweight utility pack for handling unknown type
Go to Latest
function isObjectOf
import { isObjectOf } from "https://deno.land/x/unknownutil@v3.18.0/is.ts";

Return a type predicate function that returns true if the type of x is ObjectOf<T>.

To enhance performance, users are advised to cache the return value of this function and mitigate the creation cost.

If is.OptionalOf() is specified in the predicate function, the property becomes optional.

The number of keys of x must be greater than or equal to the number of keys of predObj.

import { is } from "@core/unknownutil";

const isMyType = is.ObjectOf({
  a: is.Number,
  b: is.String,
  c: is.OptionalOf(is.Boolean),
});
const a: unknown = { a: 0, b: "a", other: "other" };
if (isMyType(a)) {
  // "other" key in `a` is ignored because of `options.strict` is `false`.
  // a is narrowed to { a: number; b: string; c?: boolean | undefined }
  const _: { a: number; b: string; c?: boolean | undefined } = a;
}

Type Parameters

T extends Record<PropertyKey, Predicate<unknown>>

Parameters

predObj: T

Returns

Predicate<ObjectOf<T>> & WithMetadata<IsObjectOfMetadata>

Type Parameters

T extends Record<PropertyKey, Predicate<unknown>>

Parameters

predObj: T
options: { strict?: boolean; }

Returns

Predicate<ObjectOf<T>> & WithMetadata<IsObjectOfMetadata>