From 38d5b0df6b4a22f4c591b2230826a211a13da4c2 Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 25 Jul 2023 13:04:28 +0900 Subject: [PATCH 1/2] :memo: Fix examples --- is.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/is.ts b/is.ts index 492a233..2a9933b 100644 --- a/is.ts +++ b/is.ts @@ -177,8 +177,8 @@ export type ObjectOf>> = FlatType< * }; * const a: unknown = { a: 0, b: "a" }; * if (is.ObjectOf(predObj)(a)) { - * // a is narrowed to { a: number, b: string, c?: boolean } - * const _: { a: number, b: string, c?: boolean } = a; + * // a is narrowed to { a: number; b: string; c?: boolean } + * const _: { a: number; b: string; c?: boolean } = a; * } * ``` */ @@ -266,9 +266,9 @@ export type OneOf = T extends (infer U)[] * import is from "./is.ts"; * * const preds = [is.Number, is.String, is.Boolean]; - * const a: unknown = { a: 0, b: "a", c: true }; + * const a: unknown = 0; * if (is.OneOf(preds)(a)) { - * // a is narrowed to number | string | boolean; + * // a is narrowed to number | string | boolean * const _: number | string | boolean = a; * } * ``` @@ -291,7 +291,7 @@ export type OptionalPredicate = Predicate & { * * const a: unknown = "a"; * if (is.OptionalOf(is.String)(a)) { - * // a is narrowed to string | undefined; + * // a is narrowed to string | undefined * const _: string | undefined = a; * } * ``` From 548be237eb2befe4fca52d8ede5d8faad1cdeb93 Mon Sep 17 00:00:00 2001 From: Milly Date: Tue, 25 Jul 2023 13:49:07 +0900 Subject: [PATCH 2/2] :+1: Add `isAllOf` --- is.ts | 31 ++++++++++++++++++++++++++++--- is_bench.ts | 21 +++++++++++++++++++++ is_test.ts | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 3 deletions(-) diff --git a/is.ts b/is.ts index 2a9933b..15a693e 100644 --- a/is.ts +++ b/is.ts @@ -255,9 +255,7 @@ export function isSymbol(x: unknown): x is symbol { return typeof x === "symbol"; } -export type OneOf = T extends (infer U)[] - ? T extends Predicate[] ? U : T - : T; +export type OneOf = T extends Predicate[] ? U : never; /** * Return a type predicate function that returns `true` if the type of `x` is `OneOf`. @@ -279,6 +277,32 @@ export function isOneOf[]>( return (x: unknown): x is OneOf => preds.some((pred) => pred(x)); } +type UnionToIntersection = + (U extends unknown ? (k: U) => void : never) extends ((k: infer I) => void) + ? I + : never; +export type AllOf = UnionToIntersection>; + +/** + * Return a type predicate function that returns `true` if the type of `x` is `AllOf`. + * + * ```ts + * import is from "./is.ts"; + * + * const preds = [is.ObjectOf({ a: is.Number }), is.ObjectOf({ b: is.String })]; + * const a: unknown = { a: 0, b: "a" }; + * if (is.AllOf(preds)(a)) { + * // a is narrowed to { a: number; b: string } + * const _: { a: number; b: string } = a; + * } + * ``` + */ +export function isAllOf[]>( + preds: T, +): Predicate> { + return (x: unknown): x is AllOf => preds.every((pred) => pred(x)); +} + export type OptionalPredicate = Predicate & { optional: true; }; @@ -323,5 +347,6 @@ export default { Nullish: isNullish, Symbol: isSymbol, OneOf: isOneOf, + AllOf: isAllOf, OptionalOf: isOptionalOf, }; diff --git a/is_bench.ts b/is_bench.ts index d1a0404..2c5bff9 100644 --- a/is_bench.ts +++ b/is_bench.ts @@ -267,6 +267,27 @@ Deno.bench({ }, }); +const predsAll = [is.String, is.Number, is.Boolean] as const; +Deno.bench({ + name: "is.AllOf", + fn: () => { + const pred = is.AllOf(predsAll); + for (const c of cs) { + pred(c); + } + }, +}); + +const isAllOfPred = is.AllOf(predsAll); +Deno.bench({ + name: "is.AllOf (pre)", + fn: () => { + for (const c of cs) { + isAllOfPred(c); + } + }, +}); + Deno.bench({ name: "is.OptionalOf", fn: () => { diff --git a/is_test.ts b/is_test.ts index 23495fd..c755bd9 100644 --- a/is_test.ts +++ b/is_test.ts @@ -7,6 +7,7 @@ import type { IsExact, } from "https://deno.land/std@0.192.0/testing/types.ts"; import is, { + isAllOf, isArray, isArrayOf, isBigInt, @@ -442,6 +443,45 @@ Deno.test("isOneOf", async (t) => { }); }); +Deno.test("isAllOf", async (t) => { + await t.step("returns proper type predicate", () => { + const preds = [ + is.ObjectOf({ a: is.Number }), + is.ObjectOf({ b: is.String }), + ]; + const a: unknown = { a: 0, b: "a" }; + if (isAllOf(preds)(a)) { + type _ = AssertTrue>; + } + }); + await t.step("returns true on all of T", () => { + const preds = [ + is.ObjectOf({ a: is.Number }), + is.ObjectOf({ b: is.String }), + ]; + assertEquals(isAllOf(preds)({ a: 0, b: "a" }), true); + }); + await t.step("returns false on non of T", async (t) => { + const preds = [ + is.ObjectOf({ a: is.Number }), + is.ObjectOf({ b: is.String }), + ]; + assertEquals( + isAllOf(preds)({ a: 0, b: 0 }), + false, + "Some properties has wrong type", + ); + assertEquals( + isAllOf(preds)({ a: 0 }), + false, + "Some properties does not exists", + ); + await testWithExamples(t, isAllOf(preds), { + excludeExamples: ["record"], + }); + }); +}); + Deno.test("isOptionalOf", async (t) => { await t.step("returns proper type predicate", () => { const a: unknown = undefined;