-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add branded types * Remove comments * Support branding inheritance, number and symbol brands * Fix tests * Add unwrap getter Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
- Loading branch information
1 parent
88e94ef
commit 7c113ab
Showing
10 changed files
with
325 additions
and
20 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
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,58 @@ | ||
// @ts-ignore TS6133 | ||
import { expect } from "https://deno.land/x/expect@v0.2.6/mod.ts"; | ||
const test = Deno.test; | ||
|
||
import { util } from "../helpers/util.ts"; | ||
import * as z from "../index.ts"; | ||
|
||
test("branded types", () => { | ||
const mySchema = z | ||
.object({ | ||
name: z.string(), | ||
}) | ||
.brand<"superschema">(); | ||
|
||
// simple branding | ||
type MySchema = z.infer<typeof mySchema>; | ||
const f1: util.AssertEqual< | ||
MySchema, | ||
{ name: string } & { [z.BRAND]: { superschema: true } } | ||
> = true; | ||
f1; | ||
const doStuff = (arg: MySchema) => arg; | ||
doStuff(mySchema.parse({ name: "hello there" })); | ||
|
||
// inheritance | ||
const extendedSchema = mySchema.brand<"subschema">(); | ||
type ExtendedSchema = z.infer<typeof extendedSchema>; | ||
const f2: util.AssertEqual< | ||
ExtendedSchema, | ||
{ name: string } & { [z.BRAND]: { superschema: true; subschema: true } } | ||
> = true; | ||
f2; | ||
doStuff(extendedSchema.parse({ name: "hello again" })); | ||
|
||
// number branding | ||
const numberSchema = z.number().brand<42>(); | ||
type NumberSchema = z.infer<typeof numberSchema>; | ||
const f3: util.AssertEqual< | ||
NumberSchema, | ||
number & { [z.BRAND]: { 42: true } } | ||
> = true; | ||
f3; | ||
|
||
// symbol branding | ||
const MyBrand: unique symbol = Symbol("hello"); | ||
type MyBrand = typeof MyBrand; | ||
const symbolBrand = z.number().brand<"sup">().brand<typeof MyBrand>(); | ||
type SymbolBrand = z.infer<typeof symbolBrand>; | ||
// number & { [z.BRAND]: { sup: true, [MyBrand]: true } } | ||
const f4: util.AssertEqual< | ||
SymbolBrand, | ||
number & { [z.BRAND]: { sup: true; [MyBrand]: true } } | ||
> = true; | ||
f4; | ||
|
||
// @ts-expect-error | ||
doStuff({ name: "hello there!" }); | ||
}); |
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
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,57 @@ | ||
// @ts-ignore TS6133 | ||
import { expect, test } from "@jest/globals"; | ||
|
||
import { util } from "../helpers/util"; | ||
import * as z from "../index"; | ||
|
||
test("branded types", () => { | ||
const mySchema = z | ||
.object({ | ||
name: z.string(), | ||
}) | ||
.brand<"superschema">(); | ||
|
||
// simple branding | ||
type MySchema = z.infer<typeof mySchema>; | ||
const f1: util.AssertEqual< | ||
MySchema, | ||
{ name: string } & { [z.BRAND]: { superschema: true } } | ||
> = true; | ||
f1; | ||
const doStuff = (arg: MySchema) => arg; | ||
doStuff(mySchema.parse({ name: "hello there" })); | ||
|
||
// inheritance | ||
const extendedSchema = mySchema.brand<"subschema">(); | ||
type ExtendedSchema = z.infer<typeof extendedSchema>; | ||
const f2: util.AssertEqual< | ||
ExtendedSchema, | ||
{ name: string } & { [z.BRAND]: { superschema: true; subschema: true } } | ||
> = true; | ||
f2; | ||
doStuff(extendedSchema.parse({ name: "hello again" })); | ||
|
||
// number branding | ||
const numberSchema = z.number().brand<42>(); | ||
type NumberSchema = z.infer<typeof numberSchema>; | ||
const f3: util.AssertEqual< | ||
NumberSchema, | ||
number & { [z.BRAND]: { 42: true } } | ||
> = true; | ||
f3; | ||
|
||
// symbol branding | ||
const MyBrand: unique symbol = Symbol("hello"); | ||
type MyBrand = typeof MyBrand; | ||
const symbolBrand = z.number().brand<"sup">().brand<typeof MyBrand>(); | ||
type SymbolBrand = z.infer<typeof symbolBrand>; | ||
// number & { [z.BRAND]: { sup: true, [MyBrand]: true } } | ||
const f4: util.AssertEqual< | ||
SymbolBrand, | ||
number & { [z.BRAND]: { sup: true; [MyBrand]: true } } | ||
> = true; | ||
f4; | ||
|
||
// @ts-expect-error | ||
doStuff({ name: "hello there!" }); | ||
}); |
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
Oops, something went wrong.