Skip to content

Commit b991bbb

Browse files
committed
fix[isRecordOf]: checks only own properties
1 parent 69bd020 commit b991bbb

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

is/record_of.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ export function isRecordOf<T, K extends PropertyKey = PropertyKey>(
3939
return rewriteName(
4040
(x: unknown): x is Record<K, T> => {
4141
if (!isRecord(x)) return false;
42-
for (const k in x) {
42+
const keys = Object.keys(x);
43+
for (const k of keys) {
4344
if (!pred(x[k])) return false;
4445
if (predKey && !predKey(k)) return false;
4546
}

is/record_of_test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,29 @@ Deno.test("isRecordOf<T>", async (t) => {
2929
assertType<Equal<typeof a, Record<PropertyKey, number>>>(true);
3030
}
3131
});
32+
33+
await t.step("checks only object's own properties", async (t) => {
34+
await t.step("returns true on T record", () => {
35+
assertEquals(
36+
isRecordOf(is.Number)(
37+
Object.assign(Object.create({ p: "ignore" }), { a: 0 }),
38+
),
39+
true,
40+
);
41+
assertEquals(
42+
isRecordOf(is.String)(
43+
Object.assign(Object.create({ p: 0 /* ignore */ }), { a: "a" }),
44+
),
45+
true,
46+
);
47+
assertEquals(
48+
isRecordOf(is.Boolean)(
49+
Object.assign(Object.create({ p: "ignore" }), { a: true }),
50+
),
51+
true,
52+
);
53+
});
54+
});
3255
});
3356

3457
Deno.test("isRecordOf<T, K>", async (t) => {
@@ -64,4 +87,34 @@ Deno.test("isRecordOf<T, K>", async (t) => {
6487
assertType<Equal<typeof a, Record<string, number>>>(true);
6588
}
6689
});
90+
91+
await t.step("checks only object's own properties", async (t) => {
92+
await t.step("returns true on T record", () => {
93+
assertEquals(
94+
isRecordOf(is.Number, is.String)(
95+
Object.assign(Object.create({ p: "ignore" }), { a: 0 }),
96+
),
97+
true,
98+
);
99+
assertEquals(
100+
isRecordOf(is.String, is.String)(
101+
Object.assign(Object.create({ p: 0 /* ignore */ }), { a: "a" }),
102+
),
103+
true,
104+
);
105+
assertEquals(
106+
isRecordOf(is.Boolean, is.String)(
107+
Object.assign(Object.create({ p: "ignore" }), { a: true }),
108+
),
109+
true,
110+
);
111+
assertEquals(
112+
isRecordOf(is.String, is.Number)(
113+
Object.assign(Object.create({ p: "ignore" }), {/* empty */}),
114+
),
115+
true,
116+
"No own properties",
117+
);
118+
});
119+
});
67120
});

0 commit comments

Comments
 (0)