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)) {
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)) {
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)) {
const _: readonly [number, string, boolean] = a;
}