Skip to content

Commit 116aeb6

Browse files
authored
Merge pull request #50 from lambdalisue/fix-type-tests
🌿 Fix type tests
2 parents 9f4c845 + cb4fd96 commit 116aeb6

File tree

1 file changed

+79
-90
lines changed

1 file changed

+79
-90
lines changed

is_test.ts

Lines changed: 79 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import {
55
import {
66
assertSnapshot,
77
} from "https://deno.land/[email protected]/testing/snapshot.ts";
8-
import type {
9-
AssertTrue,
10-
IsExact,
11-
} from "https://deno.land/[email protected]/testing/types.ts";
8+
import { assertType } from "https://deno.land/[email protected]/testing/types.ts";
129
import is, {
1310
isAllOf,
1411
isAny,
@@ -48,6 +45,11 @@ import is, {
4845
UniformTupleOf,
4946
} from "./is.ts";
5047

48+
// It seems 'IsExact' in deno_std is false positive so use `Equal` in type-challenges
49+
// https://github.com/type-challenges/type-challenges/blob/e77262dba62e9254451f661cb4fe5517ffd1d933/utils/index.d.ts#L7-L9
50+
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends
51+
(<T>() => T extends Y ? 1 : 2) ? true : false;
52+
5153
const examples = {
5254
string: ["", "Hello world"],
5355
number: [0, 1234567890],
@@ -111,16 +113,13 @@ Deno.test("PredicateType", () => {
111113
}),
112114
])),
113115
});
114-
type _ = AssertTrue<
115-
IsExact<
116-
PredicateType<typeof isArticle>,
117-
{
118-
title: string;
119-
body: string;
120-
refs: (string | { name: string; url: string })[];
121-
}
122-
>
123-
>;
116+
assertType<
117+
Equal<PredicateType<typeof isArticle>, {
118+
title: string;
119+
body: string;
120+
refs: (string | { name: string; url: string })[];
121+
}>
122+
>(true);
124123
});
125124

126125
Deno.test("isAny", async (t) => {
@@ -191,7 +190,7 @@ Deno.test("isArrayOf<T>", async (t) => {
191190
await t.step("returns proper type predicate", () => {
192191
const a: unknown = [0, 1, 2];
193192
if (isArrayOf(isNumber)(a)) {
194-
type _ = AssertTrue<IsExact<typeof a, number[]>>;
193+
assertType<Equal<typeof a, number[]>>(true);
195194
}
196195
});
197196
await t.step("returns true on T array", () => {
@@ -210,21 +209,21 @@ Deno.test("isArrayOf<T>", async (t) => {
210209
});
211210

212211
Deno.test("TupleOf<T>", () => {
213-
type _ = AssertTrue<
214-
IsExact<
212+
assertType<
213+
Equal<
215214
TupleOf<readonly [typeof is.String, typeof is.Number]>,
216215
[string, number]
217216
>
218-
>;
217+
>(true);
219218
});
220219

221220
Deno.test("ReadonlyTupleOf<T>", () => {
222-
type _ = AssertTrue<
223-
IsExact<
221+
assertType<
222+
Equal<
224223
ReadonlyTupleOf<readonly [typeof is.String, typeof is.Number]>,
225224
readonly [string, number]
226225
>
227-
>;
226+
>(true);
228227
});
229228

230229
Deno.test("isTupleOf<T>", async (t) => {
@@ -251,9 +250,7 @@ Deno.test("isTupleOf<T>", async (t) => {
251250
const predTup = [isNumber, isString, isBoolean] as const;
252251
const a: unknown = [0, "a", true];
253252
if (isTupleOf(predTup)(a)) {
254-
type _ = AssertTrue<
255-
IsExact<typeof a, [number, string, boolean]>
256-
>;
253+
assertType<Equal<typeof a, [number, string, boolean]>>(true);
257254
}
258255
});
259256
await t.step("returns true on T tuple", () => {
@@ -309,9 +306,9 @@ Deno.test("isTupleOf<T, E>", async (t) => {
309306
const predElse = is.ArrayOf(is.Number);
310307
const a: unknown = [0, "a", true, 0, 1, 2];
311308
if (isTupleOf(predTup, predElse)(a)) {
312-
type _ = AssertTrue<
313-
IsExact<typeof a, [number, string, boolean, ...number[]]>
314-
>;
309+
assertType<Equal<typeof a, [number, string, boolean, ...number[]]>>(
310+
true,
311+
);
315312
}
316313
});
317314
await t.step("returns true on T tuple", () => {
@@ -379,9 +376,7 @@ Deno.test("isReadonlyTupleOf<T>", async (t) => {
379376
const predTup = [isNumber, isString, isBoolean] as const;
380377
const a: unknown = [0, "a", true];
381378
if (isReadonlyTupleOf(predTup)(a)) {
382-
type _ = AssertTrue<
383-
IsExact<typeof a, readonly [number, string, boolean]>
384-
>;
379+
assertType<Equal<typeof a, readonly [number, string, boolean]>>(true);
385380
}
386381
});
387382
await t.step("returns true on T tuple", () => {
@@ -447,9 +442,9 @@ Deno.test("isReadonlyTupleOf<T, E>", async (t) => {
447442
const predElse = is.ArrayOf(is.Number);
448443
const a: unknown = [0, "a", true, 0, 1, 2];
449444
if (isReadonlyTupleOf(predTup, predElse)(a)) {
450-
type _ = AssertTrue<
451-
IsExact<typeof a, readonly [number, string, boolean, ...number[]]>
452-
>;
445+
assertType<
446+
Equal<typeof a, readonly [number, string, boolean, ...number[]]>
447+
>(true);
453448
}
454449
});
455450
await t.step("returns true on T tuple", () => {
@@ -501,21 +496,24 @@ Deno.test("isReadonlyTupleOf<T, E>", async (t) => {
501496
});
502497

503498
Deno.test("UniformTupleOf<N, T>", () => {
504-
type _ = AssertTrue<
505-
IsExact<
506-
UniformTupleOf<number, 5>,
507-
[number, number, number, number, number]
508-
>
509-
>;
499+
assertType<
500+
Equal<UniformTupleOf<number, 5>, [number, number, number, number, number]>
501+
>(true);
510502
});
511503

512504
Deno.test("ReadonlyUniformTupleOf<N, T>", () => {
513-
type _ = AssertTrue<
514-
IsExact<
505+
assertType<
506+
Equal<
515507
ReadonlyUniformTupleOf<number, 5>,
516-
readonly [number, number, number, number, number]
508+
readonly [
509+
number,
510+
number,
511+
number,
512+
number,
513+
number,
514+
]
517515
>
518-
>;
516+
>(true);
519517
});
520518

521519
Deno.test("isUniformTupleOf<T>", async (t) => {
@@ -530,18 +528,15 @@ Deno.test("isUniformTupleOf<T>", async (t) => {
530528
await t.step("returns proper type predicate", () => {
531529
const a: unknown = [0, 1, 2, 3, 4];
532530
if (isUniformTupleOf(5)(a)) {
533-
type _ = AssertTrue<
534-
IsExact<
535-
typeof a,
536-
[unknown, unknown, unknown, unknown, unknown]
537-
>
538-
>;
531+
assertType<
532+
Equal<typeof a, [unknown, unknown, unknown, unknown, unknown]>
533+
>(true);
539534
}
540535

541536
if (isUniformTupleOf(5, isNumber)(a)) {
542-
type _ = AssertTrue<
543-
IsExact<typeof a, [number, number, number, number, number]>
544-
>;
537+
assertType<Equal<typeof a, [number, number, number, number, number]>>(
538+
true,
539+
);
545540
}
546541
});
547542
await t.step("returns true on mono-typed T tuple", () => {
@@ -570,18 +565,18 @@ Deno.test("isReadonlyUniformTupleOf<T>", async (t) => {
570565
await t.step("returns proper type predicate", () => {
571566
const a: unknown = [0, 1, 2, 3, 4];
572567
if (isReadonlyUniformTupleOf(5)(a)) {
573-
type _ = AssertTrue<
574-
IsExact<
568+
assertType<
569+
Equal<
575570
typeof a,
576571
readonly [unknown, unknown, unknown, unknown, unknown]
577572
>
578-
>;
573+
>(true);
579574
}
580575

581576
if (isReadonlyUniformTupleOf(5, isNumber)(a)) {
582-
type _ = AssertTrue<
583-
IsExact<typeof a, readonly [number, number, number, number, number]>
584-
>;
577+
assertType<
578+
Equal<typeof a, readonly [number, number, number, number, number]>
579+
>(true);
585580
}
586581
});
587582
await t.step("returns true on mono-typed T tuple", () => {
@@ -615,9 +610,7 @@ Deno.test("isRecordOf<T>", async (t) => {
615610
await t.step("returns proper type predicate", () => {
616611
const a: unknown = { a: 0 };
617612
if (isRecordOf(isNumber)(a)) {
618-
type _ = AssertTrue<
619-
IsExact<typeof a, Record<PropertyKey, number>>
620-
>;
613+
assertType<Equal<typeof a, Record<PropertyKey, number>>>(true);
621614
}
622615
});
623616
await t.step("returns true on T record", () => {
@@ -646,9 +639,7 @@ Deno.test("isRecordOf<T, K>", async (t) => {
646639
await t.step("returns proper type predicate", () => {
647640
const a: unknown = { a: 0 };
648641
if (isRecordOf(isNumber, isString)(a)) {
649-
type _ = AssertTrue<
650-
IsExact<typeof a, Record<string, number>>
651-
>;
642+
assertType<Equal<typeof a, Record<string, number>>>(true);
652643
}
653644
});
654645
await t.step("returns true on T record", () => {
@@ -672,12 +663,12 @@ Deno.test("isRecordOf<T, K>", async (t) => {
672663
});
673664

674665
Deno.test("ObjectOf<T>", () => {
675-
type _ = AssertTrue<
676-
IsExact<
666+
assertType<
667+
Equal<
677668
ObjectOf<{ a: typeof is.Number; b: typeof is.String }>,
678669
{ a: number; b: string }
679670
>
680-
>;
671+
>(true);
681672
});
682673

683674
Deno.test("isObjectOf<T>", async (t) => {
@@ -704,9 +695,7 @@ Deno.test("isObjectOf<T>", async (t) => {
704695
};
705696
const a: unknown = { a: 0, b: "a", c: true };
706697
if (isObjectOf(predObj)(a)) {
707-
type _ = AssertTrue<
708-
IsExact<typeof a, { a: number; b: string; c: boolean }>
709-
>;
698+
assertType<Equal<typeof a, { a: number; b: string; c: boolean }>>(true);
710699
}
711700
});
712701
await t.step("returns true on T object", () => {
@@ -769,9 +758,9 @@ Deno.test("isObjectOf<T>", async (t) => {
769758
};
770759
const a: unknown = { a: 0, b: "a" };
771760
if (isObjectOf(predObj)(a)) {
772-
type _ = AssertTrue<
773-
IsExact<typeof a, { a: number; b: string | undefined; c?: boolean }>
774-
>;
761+
assertType<
762+
Equal<typeof a, { a: number; b: string | undefined; c?: boolean }>
763+
>(true);
775764
}
776765
});
777766
await t.step("returns true on T object", () => {
@@ -845,33 +834,33 @@ Deno.test("isFunction", async (t) => {
845834
await testWithExamples(t, isFunction, {
846835
validExamples: ["syncFunction", "asyncFunction"],
847836
});
848-
type _ = AssertTrue<
849-
IsExact<PredicateType<typeof isFunction>, (...args: unknown[]) => unknown>
850-
>;
837+
assertType<
838+
Equal<PredicateType<typeof isFunction>, (...args: unknown[]) => unknown>
839+
>(true);
851840
});
852841

853842
Deno.test("isSyncFunction", async (t) => {
854843
await testWithExamples(t, isSyncFunction, {
855844
validExamples: ["syncFunction"],
856845
});
857-
type _ = AssertTrue<
858-
IsExact<
846+
assertType<
847+
Equal<
859848
PredicateType<typeof isSyncFunction>,
860849
(...args: unknown[]) => unknown
861850
>
862-
>;
851+
>(true);
863852
});
864853

865854
Deno.test("isAsyncFunction", async (t) => {
866855
await testWithExamples(t, isAsyncFunction, {
867856
validExamples: ["asyncFunction"],
868857
});
869-
type _ = AssertTrue<
870-
IsExact<
858+
assertType<
859+
Equal<
871860
PredicateType<typeof isAsyncFunction>,
872861
(...args: unknown[]) => Promise<unknown>
873862
>
874-
>;
863+
>(true);
875864
});
876865

877866
Deno.test("isInstanceOf<T>", async (t) => {
@@ -901,17 +890,17 @@ Deno.test("isInstanceOf<T>", async (t) => {
901890
class Cls {}
902891
const a: unknown = new Cls();
903892
if (isInstanceOf(Cls)(a)) {
904-
type _ = AssertTrue<IsExact<typeof a, Cls>>;
893+
assertType<Equal<typeof a, Cls>>(true);
905894
}
906895

907896
const b: unknown = new Date();
908897
if (isInstanceOf(Date)(b)) {
909-
type _ = AssertTrue<IsExact<typeof b, Date>>;
898+
assertType<Equal<typeof b, Date>>(true);
910899
}
911900

912901
const c: unknown = new Promise(() => {});
913902
if (isInstanceOf(Promise)(c)) {
914-
type _ = AssertTrue<IsExact<typeof c, Promise<unknown>>>;
903+
assertType<Equal<typeof c, Promise<unknown>>>(true);
915904
}
916905
});
917906
});
@@ -962,7 +951,7 @@ Deno.test("isLiteralOf<T>", async (t) => {
962951
const pred = "hello";
963952
const a: unknown = "hello";
964953
if (isLiteralOf(pred)(a)) {
965-
type _ = AssertTrue<IsExact<typeof a, "hello">>;
954+
assertType<Equal<typeof a, "hello">>(true);
966955
}
967956
});
968957
await t.step("returns true on literal T", () => {
@@ -983,7 +972,7 @@ Deno.test("isLiteralOneOf<T>", async (t) => {
983972
const preds = ["hello", "world"] as const;
984973
const a: unknown = "hello";
985974
if (isLiteralOneOf(preds)(a)) {
986-
type _ = AssertTrue<IsExact<typeof a, "hello" | "world">>;
975+
assertType<Equal<typeof a, "hello" | "world">>(true);
987976
}
988977
});
989978
await t.step("returns true on literal T", () => {
@@ -1005,7 +994,7 @@ Deno.test("isOneOf<T>", async (t) => {
1005994
const preds = [isNumber, isString, isBoolean];
1006995
const a: unknown = [0, "a", true];
1007996
if (isOneOf(preds)(a)) {
1008-
type _ = AssertTrue<IsExact<typeof a, number | string | boolean>>;
997+
assertType<Equal<typeof a, number | string | boolean>>(true);
1009998
}
1010999
});
10111000
await t.step("returns true on one of T", () => {
@@ -1039,7 +1028,7 @@ Deno.test("isAllOf<T>", async (t) => {
10391028
];
10401029
const a: unknown = { a: 0, b: "a" };
10411030
if (isAllOf(preds)(a)) {
1042-
type _ = AssertTrue<IsExact<typeof a, { a: number; b: string }>>;
1031+
assertType<Equal<typeof a, { a: number } & { b: string }>>(true);
10431032
}
10441033
});
10451034
await t.step("returns true on all of T", () => {
@@ -1077,7 +1066,7 @@ Deno.test("isOptionalOf<T>", async (t) => {
10771066
await t.step("returns proper type predicate", () => {
10781067
const a: unknown = undefined;
10791068
if (isOptionalOf(isNumber)(a)) {
1080-
type _ = AssertTrue<IsExact<typeof a, number | undefined>>;
1069+
assertType<Equal<typeof a, number | undefined>>(true);
10811070
}
10821071
});
10831072
await t.step("with isString", async (t) => {

0 commit comments

Comments
 (0)