import { isReadonlyTupleOf } from "https://deno.land/x/unknownutil@v3.13.0/is.ts";
Return a type predicate function that returns true
if the type of x
is ReadonlyTupleOf<T>
.
import { is } from "https://deno.land/x/unknownutil@v3.13.0/mod.ts";
const predTup = [is.Number, is.String, is.Boolean] as const;
const a: unknown = [0, "a", true];
if (is.ReadonlyTupleOf(predTup)(a)) {
// a is narrowed to readonly [number, string, boolean]
const _: readonly [number, string, boolean] = a;
}
Note that predTup
must be readonly
(as const
) to infer the type of a
correctly.
TypeScript won't argues if predTup
is not readonly
because of its design limitation.
https://github.com/microsoft/TypeScript/issues/34274#issuecomment-541691353
It can also be used to check the type of the rest of the tuple like:
import { is } from "https://deno.land/x/unknownutil@v3.13.0/mod.ts";
const predTup = [is.Number, is.String, is.Boolean] as const;
const predElse = is.ArrayOf(is.Number);
const a: unknown = [0, "a", true, 0, 1, 2];
if (is.ReadonlyTupleOf(predTup, predElse)(a)) {
// a is narrowed to readonly [number, string, boolean, ...number[]]
const _: readonly [number, string, boolean, ...number[]] = a;
}
Type Parameters
T extends readonly Predicate<unknown>[]
R extends ReadonlyTupleOf<T>
Parameters
predTup: T
Type Parameters
T extends readonly Predicate<unknown>[]
E extends Predicate<unknown[]>
R extends readonly [...ReadonlyTupleOf<T>, ...PredicateType<E>]