import { isTupleOf } 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 TupleOf<T>
or TupleOf<T, E>
.
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.TupleOf([is.Number, is.String, is.Boolean]);
const a: unknown = [0, "a", true];
if (isMyType(a)) {
// a is narrowed to [number, string, boolean]
const _: [number, string, boolean] = a;
}
With predElse
:
import { is } from "@core/unknownutil";
const isMyType = is.TupleOf(
[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 [number, string, boolean, ...number[]]
const _: [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.TupleOf(predTup);
const a: unknown = [0, "a", true];
if (isMyType(a)) {
// a is narrowed to [number, string, boolean]
const _: [number, string, boolean] = a;
}
Parameters
predTup: T
Returns
Predicate<TupleOf<T>> & WithMetadata<IsTupleOfMetadata>
Returns
Predicate<[...TupleOf<T>, ...PredicateType<E>]> & WithMetadata<IsTupleOfMetadata>