Skip to content

Commit

Permalink
Refactor type checks (#1280)
Browse files Browse the repository at this point in the history
* Switch to tsx and conditional-type-checks

* Fix tests

* Remove AssertEqual

* Finish switching to ctc

* Use ecyrbe AssertEqual

* Update tests

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
colinhacks and Colin McDonnell authored Aug 8, 2022
1 parent d041b76 commit f923706
Show file tree
Hide file tree
Showing 46 changed files with 231 additions and 324 deletions.
30 changes: 9 additions & 21 deletions deno/lib/__tests__/all-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,11 @@ test("default flattened errors type inference", () => {
fieldErrors: { [P in keyof z.TypeOf<typeof Test>]?: string[] | undefined };
};

const t1: util.AssertEqual<
z.inferFlattenedErrors<typeof Test>,
TestTypeErrors
> = true;
const t2: util.AssertEqual<
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(true);
util.assertEqual<
z.inferFlattenedErrors<typeof Test, { message: string }>,
TestTypeErrors
> = false;
[t1, t2];
>(false);
});

test("custom flattened errors type inference", () => {
Expand All @@ -43,19 +39,15 @@ test("custom flattened errors type inference", () => {
};
};

const t1: util.AssertEqual<
z.inferFlattenedErrors<typeof Test>,
TestTypeErrors
> = false;
const t2: util.AssertEqual<
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(false);
util.assertEqual<
z.inferFlattenedErrors<typeof Test, { message: string; code: number }>,
TestTypeErrors
> = true;
const t3: util.AssertEqual<
>(true);
util.assertEqual<
z.inferFlattenedErrors<typeof Test, { message: string }>,
TestTypeErrors
> = false;
[t1, t2, t3];
>(false);
});

test("form errors type inference", () => {
Expand All @@ -64,11 +56,7 @@ test("form errors type inference", () => {
fieldErrors: { [P in keyof z.TypeOf<typeof Test>]?: string[] | undefined };
};

const t1: util.AssertEqual<
z.inferFlattenedErrors<typeof Test>,
TestTypeErrors
> = true;
[t1];
util.assertEqual<z.inferFlattenedErrors<typeof Test>, TestTypeErrors>(true);
});

test(".flatten() type assertion", () => {
Expand Down
6 changes: 2 additions & 4 deletions deno/lib/__tests__/anyunknown.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ test("check any inference", () => {
t1.optional();
t1.nullable();
type t1 = z.infer<typeof t1>;
const f1: util.AssertEqual<t1, any> = true;
expect(f1).toBeTruthy();
util.assertEqual<t1, any>(true);
});

test("check unknown inference", () => {
const t1 = z.unknown();
t1.optional();
t1.nullable();
type t1 = z.infer<typeof t1>;
const f1: util.AssertEqual<t1, unknown> = true;
expect(f1).toBeTruthy();
util.assertEqual<t1, unknown>(true);
});

test("check never inference", () => {
Expand Down
7 changes: 3 additions & 4 deletions deno/lib/__tests__/array.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@ const intNum = z.string().array().nonempty();
const nonEmptyMax = z.string().array().nonempty().max(2);

type t1 = z.infer<typeof nonEmptyMax>;
const f1: util.AssertEqual<[string, ...string[]], t1> = true;
f1;
util.assertEqual<[string, ...string[]], t1>(true);

type t2 = z.infer<typeof minTwo>;
const f2: util.AssertEqual<string[], t2> = true;
f2;
util.assertEqual<string[], t2>(true);

test("passing validations", () => {
minTwo.parse(["a", "a"]);
Expand Down
5 changes: 2 additions & 3 deletions deno/lib/__tests__/base.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ test("type guard", () => {
});
type t1 = z.input<typeof s1>;

const data: any = "asdf";
const data = { stringToNumber: "asdf" };
const parsed = s1.safeParse(data);
if (parsed.success) {
const f1: util.AssertEqual<typeof data, t1> = true;
f1;
util.assertEqual<typeof data, t1>(true);
}
});

Expand Down
30 changes: 9 additions & 21 deletions deno/lib/__tests__/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ test("default with transform", () => {
);

type inp = z.input<typeof stringWithDefault>;
const f1: util.AssertEqual<inp, string | undefined> = true;
util.assertEqual<inp, string | undefined>(true);
type out = z.output<typeof stringWithDefault>;
const f2: util.AssertEqual<out, string> = true;
f1;
f2;
util.assertEqual<out, string>(true);
});

test("default on existing optional", () => {
Expand All @@ -39,22 +37,18 @@ test("default on existing optional", () => {
);

type inp = z.input<typeof stringWithDefault>;
const f1: util.AssertEqual<inp, string | undefined> = true;
util.assertEqual<inp, string | undefined>(true);
type out = z.output<typeof stringWithDefault>;
const f2: util.AssertEqual<out, string> = true;
f1;
f2;
util.assertEqual<out, string>(true);
});

test("optional on default", () => {
const stringWithDefault = z.string().default("asdf").optional();

type inp = z.input<typeof stringWithDefault>;
const f1: util.AssertEqual<inp, string | undefined> = true;
util.assertEqual<inp, string | undefined>(true);
type out = z.output<typeof stringWithDefault>;
const f2: util.AssertEqual<out, string | undefined> = true;
f1;
f2;
util.assertEqual<out, string | undefined>(true);
});

test("complex chain example", () => {
Expand All @@ -74,8 +68,7 @@ test("removeDefault", () => {
const stringWithRemovedDefault = z.string().default("asdf").removeDefault();

type out = z.output<typeof stringWithRemovedDefault>;
const f2: util.AssertEqual<out, string> = true;
f2;
util.assertEqual<out, string>(true);
});

test("nested", () => {
Expand All @@ -84,14 +77,9 @@ test("nested", () => {
inner: undefined,
});
type input = z.input<typeof outer>;
const f1: util.AssertEqual<
input,
{ inner?: string | undefined } | undefined
> = true;
util.assertEqual<input, { inner?: string | undefined } | undefined>(true);
type out = z.output<typeof outer>;
const f2: util.AssertEqual<out, { inner: string }> = true;
f1;
f2;
util.assertEqual<out, { inner: string }>(true);
expect(outer.parse(undefined)).toEqual({ inner: "asdf" });
expect(outer.parse({})).toEqual({ inner: "asdf" });
expect(outer.parse({ inner: undefined })).toEqual({ inner: "asdf" });
Expand Down
7 changes: 3 additions & 4 deletions deno/lib/__tests__/enum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ test("create enum", () => {
test("infer enum", () => {
const MyEnum = z.enum(["Red", "Green", "Blue"]);
type MyEnum = z.infer<typeof MyEnum>;
const t1: util.AssertEqual<MyEnum, "Red" | "Green" | "Blue"> = true;
[t1];
util.assertEqual<MyEnum, "Red" | "Green" | "Blue">(true);
});

test("get options", () => {
Expand All @@ -27,8 +26,8 @@ test("readonly enum", () => {
const HTTP_SUCCESS = ["200", "201"] as const;
const arg = z.enum(HTTP_SUCCESS);
type arg = z.infer<typeof arg>;
const f1: util.AssertEqual<arg, "200" | "201"> = true;
f1;
util.assertEqual<arg, "200" | "201">(true);

arg.parse("201");
expect(() => arg.parse("202")).toThrow();
});
Expand Down
13 changes: 8 additions & 5 deletions deno/lib/__tests__/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ test("function inference 1", () => {
test("args method", () => {
const t1 = z.function();
type t1 = z.infer<typeof t1>;
util.assertEqual<t1, () => void>(true);
util.assertEqual<t1, (...args_1: unknown[]) => unknown>(true);

const t2 = t1.args(z.string());
type t2 = z.infer<typeof t2>;
util.assertEqual<t2, (arg: string) => void>(true);
util.assertEqual<t2, (arg: string, ...args_1: unknown[]) => unknown>(true);

const t3 = t2.returns(z.boolean());
type t3 = z.infer<typeof t3>;
util.assertEqual<t3, (arg: string) => boolean>(true);
util.assertEqual<t3, (arg: string, ...args_1: unknown[]) => boolean>(true);
});

const args2 = z.tuple([
Expand Down Expand Up @@ -219,7 +219,7 @@ test("inference with transforms", () => {

util.assertEqual<
typeof myFunc,
(arg: string) => { val: number; extra: string }
(arg: string, ...args_1: unknown[]) => { val: number; extra: string }
>(true);
});

Expand All @@ -233,5 +233,8 @@ test("fallback to OuterTypeOfFunction", () => {
return { arg: val, arg2: false };
});

util.assertEqual<typeof myFunc, (arg: string) => number>(true);
util.assertEqual<
typeof myFunc,
(arg: string, ...args_1: unknown[]) => number
>(true);
});
3 changes: 1 addition & 2 deletions deno/lib/__tests__/instanceof.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ test("instanceof", async () => {
/Input not instance of Test/
);

const f1: util.AssertEqual<Test, z.infer<typeof TestSchema>> = true;
expect(f1).toBeTruthy();
util.assertEqual<Test, z.infer<typeof TestSchema>>(true);
});

test("instanceof fatal", () => {
Expand Down
3 changes: 1 addition & 2 deletions deno/lib/__tests__/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const stringMap = z.map(z.string(), z.string());
type stringMap = z.infer<typeof stringMap>;

test("type inference", () => {
const f1: util.AssertEqual<stringMap, Map<string, string>> = true;
f1;
util.assertEqual<stringMap, Map<string, string>>(true);
});

test("valid parse", () => {
Expand Down
9 changes: 3 additions & 6 deletions deno/lib/__tests__/nativeEnum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ test("nativeEnum test with consts", () => {
fruitEnum.parse("banana");
fruitEnum.parse(Fruits.Apple);
fruitEnum.parse(Fruits.Banana);
const t1: util.AssertEqual<fruitEnum, "apple" | "banana"> = true;
[t1];
util.assertEqual<fruitEnum, "apple" | "banana">(true);
});

test("nativeEnum test with real enum", () => {
Expand All @@ -32,8 +31,7 @@ test("nativeEnum test with real enum", () => {
fruitEnum.parse("banana");
fruitEnum.parse(Fruits.Apple);
fruitEnum.parse(Fruits.Banana);
const t1: util.AssertEqual<fruitEnum, Fruits> = true;
[t1];
util.assertIs<fruitEnum extends Fruits ? true : false>(true);
});

test("nativeEnum test with const with numeric keys", () => {
Expand All @@ -48,8 +46,7 @@ test("nativeEnum test with const with numeric keys", () => {
fruitEnum.parse(20);
fruitEnum.parse(FruitValues.Apple);
fruitEnum.parse(FruitValues.Banana);
const t1: util.AssertEqual<fruitEnum, 10 | 20> = true;
[t1];
util.assertEqual<fruitEnum, 10 | 20>(true);
});

test("from enum", () => {
Expand Down
Loading

0 comments on commit f923706

Please sign in to comment.