Skip to content

Commit 587341e

Browse files
committed
🌿 Improve type tests
1 parent 3e0914c commit 587341e

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

is_test.ts

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import {
22
assertEquals,
33
assertStrictEquals,
44
} from "https://deno.land/[email protected]/testing/asserts.ts";
5+
import type {
6+
AssertTrue,
7+
IsExact,
8+
} from "https://deno.land/[email protected]/testing/types.ts";
59
import is, {
610
isArray,
711
isArrayOf,
@@ -86,7 +90,7 @@ Deno.test("isArrayOf<T>", async (t) => {
8690
await t.step("returns proper type predicate", () => {
8791
const a: unknown = [0, 1, 2];
8892
if (isArrayOf(isNumber)(a)) {
89-
const _: number[] = a;
93+
type _ = AssertTrue<IsExact<typeof a, number[]>>;
9094
}
9195
});
9296
await t.step("returns true on T array", () => {
@@ -106,7 +110,9 @@ Deno.test("isTupleOf<T>", async (t) => {
106110
const predTup = [isNumber, isString, isBoolean] as const;
107111
const a: unknown = [0, "a", true];
108112
if (isTupleOf(predTup)(a)) {
109-
const _: readonly [number, string, boolean] = a;
113+
type _ = AssertTrue<
114+
IsExact<typeof a, readonly [number, string, boolean]>
115+
>;
110116
}
111117
});
112118
await t.step("returns true on T tuple", () => {
@@ -125,11 +131,18 @@ Deno.test("isUniformTupleOf<T>", async (t) => {
125131
await t.step("returns proper type predicate", () => {
126132
const a: unknown = [0, 1, 2, 3, 4];
127133
if (isUniformTupleOf(5)(a)) {
128-
const _: readonly [unknown, unknown, unknown, unknown, unknown] = a;
134+
type _ = AssertTrue<
135+
IsExact<
136+
typeof a,
137+
readonly [unknown, unknown, unknown, unknown, unknown]
138+
>
139+
>;
129140
}
130141

131142
if (isUniformTupleOf(5, isNumber)(a)) {
132-
const _: readonly [number, number, number, number, number] = a;
143+
type _ = AssertTrue<
144+
IsExact<typeof a, readonly [number, number, number, number, number]>
145+
>;
133146
}
134147
});
135148
await t.step("returns true on mono-typed T tuple", () => {
@@ -151,7 +164,9 @@ Deno.test("isRecordOf<T>", async (t) => {
151164
await t.step("returns proper type predicate", () => {
152165
const a: unknown = { a: 0 };
153166
if (isRecordOf(isNumber)(a)) {
154-
const _: Record<string | number | symbol, number> = a;
167+
type _ = AssertTrue<
168+
IsExact<typeof a, Record<string | number | symbol, number>>
169+
>;
155170
}
156171
});
157172
await t.step("returns true on T record", () => {
@@ -175,7 +190,9 @@ Deno.test("isObjectOf<T>", async (t) => {
175190
};
176191
const a: unknown = { a: 0, b: "a", c: true };
177192
if (isObjectOf(predObj)(a)) {
178-
const _: { a: number; b: string; c: boolean } = a;
193+
type _ = AssertTrue<
194+
IsExact<typeof a, { a: number; b: string; c: boolean }>
195+
>;
179196
}
180197
});
181198
await t.step("returns true on T object", () => {
@@ -239,17 +256,17 @@ Deno.test("isInstanceOf<T>", async (t) => {
239256
class Cls {}
240257
const a: unknown = new Cls();
241258
if (isInstanceOf(Cls)(a)) {
242-
const _: Cls = a;
259+
type _ = AssertTrue<IsExact<typeof a, Cls>>;
243260
}
244261

245262
const b: unknown = new Date();
246263
if (isInstanceOf(Date)(b)) {
247-
const _: Date = b;
264+
type _ = AssertTrue<IsExact<typeof b, Date>>;
248265
}
249266

250267
const c: unknown = new Promise(() => {});
251268
if (isInstanceOf(Promise)(c)) {
252-
const _: Promise<unknown> = c;
269+
type _ = AssertTrue<IsExact<typeof c, Promise<unknown>>>;
253270
}
254271
});
255272
});
@@ -275,7 +292,7 @@ Deno.test("isOneOf<T>", async (t) => {
275292
const preds = [isNumber, isString, isBoolean];
276293
const a: unknown = [0, "a", true];
277294
if (isOneOf(preds)(a)) {
278-
const _: number | string | boolean = a;
295+
type _ = AssertTrue<IsExact<typeof a, number | string | boolean>>;
279296
}
280297
});
281298
await t.step("returns true on one of T", () => {

0 commit comments

Comments
 (0)