Skip to main content
Module

x/unknownutil/mod.ts>isPartialOf

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

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

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

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

const isMyType = is.PartialOf(is.ObjectOf({
  a: is.Number,
  b: is.UnionOf([is.String, is.Undefined]),
  c: is.OptionalOf(is.Boolean),
}));
const a: unknown = { a: undefined, other: "other" };
if (isMyType(a)) {
  // The "other" key in `a` is ignored.
  // 'a' is narrowed to { a?: number | undefined; b?: string | undefined; c?: boolean | undefined }
  const _: { a?: number | undefined; b?: string | undefined; c?: boolean | undefined } = a;
}

Type Parameters

T extends Record<PropertyKey, unknown>

Parameters

pred: Predicate<T> & WithMetadata<IsObjectOfMetadata>

Returns

Predicate<FlatType<Partial<T>>> & WithMetadata<IsObjectOfMetadata>