From ca0ebf8ebd678d51d467a6b4202a750a31c9a408 Mon Sep 17 00:00:00 2001 From: Alisue Date: Wed, 14 Aug 2024 02:22:09 +0900 Subject: [PATCH 1/3] fix: use `any` instead of `unknown` to fix types --- _annotation.ts | 3 ++- _funcutil.ts | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/_annotation.ts b/_annotation.ts index 73fa314..fb43034 100644 --- a/_annotation.ts +++ b/_annotation.ts @@ -1,6 +1,7 @@ import type { Predicate } from "./type.ts"; -export type Fn = (...args: unknown[]) => unknown; +// deno-lint-ignore no-explicit-any +export type Fn = (...args: any[]) => unknown; export function annotate( fn: F, diff --git a/_funcutil.ts b/_funcutil.ts index 54e1cf6..7b03f60 100644 --- a/_funcutil.ts +++ b/_funcutil.ts @@ -3,7 +3,8 @@ import { inspect } from "./_inspect.ts"; /** * Rewrite the function name. */ -export function rewriteName unknown>( +// deno-lint-ignore no-explicit-any +export function rewriteName unknown>( fn: F, name: string, ...args: unknown[] From 30fe0beb7f60f6e9613af899556a50e8ba6b31bd Mon Sep 17 00:00:00 2001 From: Alisue Date: Wed, 14 Aug 2024 02:22:52 +0900 Subject: [PATCH 2/3] fix(array_of): pass only required arguments to `pred` --- is/array_of.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/is/array_of.ts b/is/array_of.ts index e9b91a7..abd4993 100644 --- a/is/array_of.ts +++ b/is/array_of.ts @@ -23,7 +23,7 @@ export function isArrayOf( pred: Predicate, ): Predicate { return rewriteName( - (x: unknown): x is T[] => isArray(x) && x.every(pred), + (x: unknown): x is T[] => isArray(x) && x.every((v) => pred(v)), "isArrayOf", pred, ); From 456e2b90b40366dd5f2d6974332e309dca36cbec Mon Sep 17 00:00:00 2001 From: Alisue Date: Wed, 14 Aug 2024 02:50:04 +0900 Subject: [PATCH 3/3] refactor(is/object_of): add internal `isObject` function Co-authored-by: Milly --- is/object_of.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/is/object_of.ts b/is/object_of.ts index 7121e64..7be8ca9 100644 --- a/is/object_of.ts +++ b/is/object_of.ts @@ -43,22 +43,22 @@ export function isObjectOf< ...Object.keys(predObj), ...Object.getOwnPropertySymbols(predObj), ].map((k) => [k, predObj[k]]); - return annotate( - rewriteName( - (x: unknown): x is ObjectOf => { - if ( - x == null || - typeof x !== "object" && typeof x !== "function" || - Array.isArray(x) - ) return false; - return preds.every(([k, pred]) => pred((x as T)[k])); - }, - "isObjectOf", - predObj, - ), - "predObj", + const pred = rewriteName( + (x): x is ObjectOf => { + if (!isObject(x)) return false; + return preds.every(([k, pred]) => pred(x[k])); + }, + "isObjectOf", predObj, ); + return annotate(pred, "predObj", predObj); +} + +function isObject(x: unknown): x is Record { + if (x == null) return false; + if (typeof x !== "object" && typeof x !== "function") return false; + if (Array.isArray(x)) return false; + return true; } type ObjectOf>> = FlatType<