Skip to content

Commit

Permalink
refactor(is/object_of): add internal isRecordT function
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed Aug 13, 2024
1 parent 30fe0be commit 141c403
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions is/object_of.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,24 @@ export function isObjectOf<
...Object.keys(predObj),
...Object.getOwnPropertySymbols(predObj),
].map((k) => [k, predObj[k]]);
return annotate(
rewriteName(
(x: unknown): x is ObjectOf<T> => {
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<T> => {
if (!isRecordT<T>(x)) return false;
return preds.every(([k, pred]) => pred(x[k]));
},
"isObjectOf",
predObj,
);
return annotate(pred, "predObj", predObj);
}

function isRecordT<T extends Record<PropertyKey, Predicate<unknown>>>(
x: unknown,
): x is T {
if (x == null) return false;
if (typeof x !== "object" && typeof x !== "function") return false;
if (Array.isArray(x)) return false;
return true;
}

type ObjectOf<T extends Record<PropertyKey, Predicate<unknown>>> = FlatType<
Expand Down

0 comments on commit 141c403

Please sign in to comment.