import { isRecordObjectOf } from "https://deno.land/x/unknownutil@v3.18.1/mod.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;
}
Returns
Predicate<Record<K, T>> & WithMetadata<IsRecordObjectOfMetadata>