import { isIntersectionOf } 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 IntersectionOf<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.IntersectionOf([
is.ObjectOf({ a: is.Number }),
is.ObjectOf({ b: is.String }),
]);
const a: unknown = { a: 0, b: "a" };
if (isMyType(a)) {
// a is narrowed to { a: number } & { b: string }
const _: { a: number } & { b: string } = 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 preds
. If a type error occurs, try adding as const
as follows:
import { is } from "@core/unknownutil";
const preds = [
is.ObjectOf({ a: is.Number }),
is.ObjectOf({ b: is.String }),
] as const
const isMyType = is.IntersectionOf(preds);
const a: unknown = { a: 0, b: "a" };
if (isMyType(a)) {
// a is narrowed to { a: number } & { b: string }
const _: { a: number } & { b: string } = a;
}
Type Parameters
T extends readonly [Predicate<unknown> & WithMetadata<IsObjectOfMetadata>, ...(Predicate<unknown> & WithMetadata<IsObjectOfMetadata>)[]]
Parameters
preds: T
Returns
Predicate<IntersectionOf<T>> & WithMetadata<IsObjectOfMetadata>
Parameters
preds: T
Returns
Predicate<IntersectionOf<T>> & WithMetadata<IsIntersectionOfMetadata>