Module

x/unknownutil/is.ts>isReadonlyTupleOf

🦕 A lightweight utility pack for handling unknown type
Latest
function isReadonlyTupleOf
Deprecated
Deprecated

Use is.ReadonlyOf(is.TupleOf(...)) instead.

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.ReadonlyTupleOf([is.Number, is.String, is.Boolean]);
const a: unknown = [0, "a", true];
if (isMyType(a)) {
// a is narrowed to readonly [number, string, boolean]
const _: readonly [number, string, boolean] = a;
}

With predElse:

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

const isMyType = is.ReadonlyTupleOf(
[is.Number, is.String, is.Boolean],
is.ArrayOf(is.Number),
);
const a: unknown = [0, "a", true, 0, 1, 2];
if (isMyType(a)) {
// a is narrowed to readonly [number, string, boolean, ...number[]]
const _: readonly [number, string, boolean, ...number[]] = a;
}

Depending on the version of TypeScript and how values are provided, it may be necessary to add as const to the array used as predTup. If a type error occurs, try adding as const as follows:

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

const predTup = [is.Number, is.String, is.Boolean] as const;
const isMyType = is.ReadonlyTupleOf(predTup);
const a: unknown = [0, "a", true];
if (isMyType(a)) {
// a is narrowed to readonly [number, string, boolean]
const _: readonly [number, string, boolean] = a;
}
import { isReadonlyTupleOf } from "https://dotland.deno.dev/x/unknownutil@v3.18.1/is.ts";

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

Type Parameters

T extends readonly [Predicate<unknown>, ...Predicate<unknown>[]]

Parameters

predTup: T

Returns

Predicate<Readonly<TupleOf<T>>> & WithMetadata<IsReadonlyOfMetadata>

Type Parameters

T extends readonly [Predicate<unknown>, ...Predicate<unknown>[]]
E extends Predicate<unknown[]>

Parameters

predTup: T
predElse: E

Returns

Predicate<Readonly<[...TupleOf<T>, ...PredicateType<E>]>> & WithMetadata<IsReadonlyOfMetadata>