Skip to content

Commit 0f9904a

Browse files
committed
test[isIntersectionOf]: discover symbol properties in annotated predObj
1 parent a7a757b commit 0f9904a

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

is/__snapshots__/intersection_of_test.ts.snap

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,19 @@ snapshot[`isIntersectionOf<T> > returns properly named predicate function 3`] =
1515
isObjectOf({b: isString})
1616
])"
1717
`;
18+
19+
snapshot[`isIntersectionOf<T> > with symbol properties > returns properly named predicate function 1`] = `"isString"`;
20+
21+
snapshot[`isIntersectionOf<T> > with symbol properties > returns properly named predicate function 2`] = `
22+
"isObjectOf({
23+
a: isNumber,
24+
Symbol(b): isString
25+
})"
26+
`;
27+
28+
snapshot[`isIntersectionOf<T> > with symbol properties > returns properly named predicate function 3`] = `
29+
"isIntersectionOf([
30+
isFunction,
31+
isObjectOf({Symbol(b): isString})
32+
])"
33+
`;

is/intersection_of_test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,79 @@ Deno.test("isIntersectionOf<T>", async (t) => {
7777
>(true);
7878
}
7979
});
80+
81+
await t.step("with symbol properties", async (t) => {
82+
const b = Symbol("b");
83+
const objPreds = [
84+
is.ObjectOf({ a: is.Number }),
85+
is.ObjectOf({ [b]: is.String }),
86+
] as const;
87+
const mixPreds = [
88+
is.Function,
89+
is.ObjectOf({ [b]: is.String }),
90+
] as const;
91+
92+
await t.step("returns properly named predicate function", async (t) => {
93+
assertEquals(typeof isIntersectionOf([is.String]), "function");
94+
await assertSnapshot(t, isIntersectionOf([is.String]).name);
95+
await assertSnapshot(t, isIntersectionOf(objPreds).name);
96+
await assertSnapshot(t, isIntersectionOf(mixPreds).name);
97+
});
98+
99+
await t.step("returns true on all of T", () => {
100+
const f = Object.assign(() => void 0, { [b]: "a" });
101+
assertEquals(isIntersectionOf([is.String])("a"), true);
102+
assertEquals(isIntersectionOf(objPreds)({ a: 0, [b]: "a" }), true);
103+
assertEquals(isIntersectionOf(mixPreds)(f), true);
104+
});
105+
106+
await t.step("returns false on non of T", () => {
107+
const f = Object.assign(() => void 0, { [b]: "a" });
108+
assertEquals(isIntersectionOf(objPreds)("a"), false);
109+
assertEquals(isIntersectionOf(mixPreds)({ a: 0, [b]: "a" }), false);
110+
assertEquals(isIntersectionOf([is.String])(f), false);
111+
});
112+
113+
await t.step("predicated type is correct", () => {
114+
const a: unknown = undefined;
115+
116+
if (isIntersectionOf([is.String])(a)) {
117+
assertType<Equal<typeof a, string>>(true);
118+
}
119+
120+
if (isIntersectionOf(objPreds)(a)) {
121+
assertType<Equal<typeof a, { a: number } & { [b]: string }>>(true);
122+
}
123+
124+
if (isIntersectionOf(mixPreds)(a)) {
125+
assertType<
126+
Equal<
127+
typeof a,
128+
& ((...args: unknown[]) => unknown)
129+
& { [b]: string }
130+
>
131+
>(true);
132+
}
133+
});
134+
135+
await t.step("predicated type is correct (#68)", () => {
136+
const a: unknown = undefined;
137+
const pred = isIntersectionOf([
138+
is.ObjectOf({ id: is.String }),
139+
is.UnionOf([
140+
is.ObjectOf({ result: is.String }),
141+
is.ObjectOf({ error: is.String }),
142+
]),
143+
]);
144+
145+
if (pred(a)) {
146+
assertType<
147+
Equal<
148+
typeof a,
149+
{ id: string } & ({ result: string } | { error: string })
150+
>
151+
>(true);
152+
}
153+
});
154+
});
80155
});

0 commit comments

Comments
 (0)