Skip to main content
Module

x/unknownutil/is.ts>isMapOf

🦕 A lightweight utility pack for handling unknown type
Go to Latest
function isMapOf
import { isMapOf } 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 Map<K, 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.MapOf(is.Number);
const a: unknown = new Map([["a", 0], ["b", 1]]);
if (isMyType(a)) {
  // a is narrowed to Map<unknown, number>
  const _: Map<unknown, number> = a;
}

With predicate function for keys:

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

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

Parameters

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

Returns

Predicate<Map<K, T>> & WithMetadata<IsMapOfMetadata>