-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(guard) reintroduce check as guard
discussion: unlike check, guard is only visible when Output extend Input
- Loading branch information
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expectTypeOf, test } from "vitest"; | ||
import { z } from "../index.ts"; | ||
|
||
describe("guard", () => { | ||
test("work as a type guard when Output extends Input", () => { | ||
expectTypeOf<true>().toEqualTypeOf<true>(); | ||
}); | ||
test("work as a type guard when Output *does* extend Input", () => { | ||
const inputExtendsOutputSchema = z.object({ | ||
a: z.string(), | ||
b: z.enum(["x", "y"]).transform((arg) => arg as string), | ||
}); | ||
const val = null as unknown; | ||
if (inputExtendsOutputSchema.guard(val)) { | ||
expectTypeOf(val).toEqualTypeOf<{ a: string; b: string }>(); | ||
} else { | ||
expectTypeOf(val).toEqualTypeOf<unknown>(); | ||
} | ||
}); | ||
|
||
test("to be unavailable when Output *does not* extend Input", () => { | ||
const inputDiffersFromOutputSchema = z | ||
.string() | ||
.transform((arg) => parseFloat(arg)); | ||
const val = null as unknown; | ||
// @ts-expect-error - compile error as Input does not extend Output | ||
inputDiffersFromOutputSchema.guard(val); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, expectTypeOf, test } from "vitest"; | ||
import { z } from "../index"; | ||
|
||
describe("guard", () => { | ||
test("work as a type guard when Output extends Input", () => { | ||
expectTypeOf<true>().toEqualTypeOf<true>(); | ||
}); | ||
test("work as a type guard when Output *does* extend Input", () => { | ||
const inputExtendsOutputSchema = z.object({ | ||
a: z.string(), | ||
b: z.enum(["x", "y"]).transform((arg) => arg as string), | ||
}); | ||
const val = null as unknown; | ||
if (inputExtendsOutputSchema.guard(val)) { | ||
expectTypeOf(val).toEqualTypeOf<{ a: string; b: string }>(); | ||
} else { | ||
expectTypeOf(val).toEqualTypeOf<unknown>(); | ||
} | ||
}); | ||
|
||
test("to be unavailable when Output *does not* extend Input", () => { | ||
const inputDiffersFromOutputSchema = z | ||
.string() | ||
.transform((arg) => parseFloat(arg)); | ||
const val = null as unknown; | ||
// @ts-expect-error - compile error as Input does not extend Output | ||
inputDiffersFromOutputSchema.guard(val); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters