Skip to main content
Module

x/unknownutil/is.ts>isRecordObjectOf

🦕 A lightweight utility pack for handling unknown type
Go to Latest
function isRecordObjectOf
import { isRecordObjectOf } 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 an Object instance that satisfies Record<K, T>.

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

Note that this function check if the x is an instance of Object. Use isRecordOf instead if you want to check if the x satisfies the Record<K, T> type.

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

const isMyType = is.RecordObjectOf(is.Number);
const a: unknown = {"a": 0, "b": 1};
if (isMyType(a)) {
  // a is narrowed to Record<PropertyKey, number>
  const _: Record<PropertyKey, number> = a;
}

With predicate function for keys:

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

const isMyType = is.RecordObjectOf(is.Number, is.String);
const a: unknown = {"a": 0, "b": 1};
if (isMyType(a)) {
  // a is narrowed to Record<string, number>
  const _: Record<string, number> = a;
}

Type Parameters

T
optional
K extends PropertyKey = PropertyKey

Parameters

pred: Predicate<T>
optional
predKey: Predicate<K>

Returns

Predicate<Record<K, T>> & WithMetadata<IsRecordObjectOfMetadata>