|
| 1 | +--- |
| 2 | +title: ArkType 2.2 Twitter/X Thread |
| 3 | +--- |
| 4 | + |
| 5 | +# ArkType 2.2 Twitter/X Thread |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +**1/** |
| 10 | + |
| 11 | +ArkType 2.2 is out. |
| 12 | + |
| 13 | +This is our biggest release since 2.0- validated functions, type-safe regex, bidirectional JSON Schema, and universal schema interop. |
| 14 | + |
| 15 | +Here's what's new 🧵 |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +**2/** |
| 20 | + |
| 21 | +type.fn |
| 22 | + |
| 23 | +Define runtime-validated functions using the same syntax you already know. |
| 24 | + |
| 25 | +```ts |
| 26 | +const len = type.fn("string | unknown[]", ":", "number")(s => s.length) |
| 27 | + |
| 28 | +len("foo") // 3 |
| 29 | +len([1, 2]) // 2 |
| 30 | +``` |
| 31 | + |
| 32 | +Defaults, optionals, variadics- it all works. |
| 33 | + |
| 34 | +--- |
| 35 | + |
| 36 | +**3/** |
| 37 | + |
| 38 | +Type-safe regex via arkregex |
| 39 | + |
| 40 | +Regex literals in your definitions now carry full type inference. x-prefix parses capture groups at runtime. |
| 41 | + |
| 42 | +```ts |
| 43 | +const T = type({ |
| 44 | + birthday: "x/^(?<month>\\d{2})-(?<day>\\d{2})-(?<year>\\d{4})$/" |
| 45 | +}) |
| 46 | + |
| 47 | +T.assert({ birthday: "05-21-1993" }).birthday.groups.month // "05" |
| 48 | +``` |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +**4/** |
| 53 | + |
| 54 | +Standard Schema validators can now be embedded directly in ArkType definitions. |
| 55 | + |
| 56 | +Zod, Valibot, or anything else that implements the spec. |
| 57 | + |
| 58 | +```ts |
| 59 | +const User = type({ |
| 60 | + name: "string", |
| 61 | + age: "number", |
| 62 | + address: { street: "string" } |
| 63 | +}) |
| 64 | +``` |
| 65 | + |
| 66 | +ArkType as a universal composition layer. |
| 67 | + |
| 68 | +--- |
| 69 | + |
| 70 | +**5/** |
| 71 | + |
| 72 | +@ark/json-schema |
| 73 | + |
| 74 | +Bidirectional JSON Schema conversion. Parse JSON Schema into ArkType Types, and convert Types to JSON Schema. |
| 75 | + |
| 76 | +Huge thanks to @TizzySaurus for building this one. |
| 77 | + |
| 78 | +toJsonSchema() now supports configurable fallbacks, draft-07/2020-12 targets, and cyclic types. |
| 79 | + |
| 80 | +--- |
| 81 | + |
| 82 | +**6/** |
| 83 | + |
| 84 | +Configurable toJsonSchema |
| 85 | + |
| 86 | +Not everything in ArkType maps cleanly to JSON Schema. Now you can handle incompatibilities your way. |
| 87 | + |
| 88 | +```ts |
| 89 | +const T = type({ |
| 90 | + birthday: "Date" |
| 91 | +}) |
| 92 | + |
| 93 | +T.toJsonSchema({ |
| 94 | + fallback: { |
| 95 | + date: ctx => ({ ...ctx.base, type: "string", format: "date-time" }), |
| 96 | + default: ctx => ctx.base |
| 97 | + } |
| 98 | +}) |
| 99 | +``` |
| 100 | + |
| 101 | +11 granular fallback codes. |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +**7/** |
| 106 | + |
| 107 | +select + configure |
| 108 | + |
| 109 | +Query your types by node kind and predicate. Use the same selectors to configure specific references. |
| 110 | + |
| 111 | +```ts |
| 112 | +const myType = type("1 < number < 10") |
| 113 | + |
| 114 | +const minNodes = myType.select("min") |
| 115 | +const exclusiveMins = minNodes.filter(n => n.exclusive) |
| 116 | + |
| 117 | +myType.configure( |
| 118 | + { description: "a special domain" }, |
| 119 | + "domain" |
| 120 | +) |
| 121 | +``` |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +**8/** |
| 126 | + |
| 127 | +Improved type.declare |
| 128 | + |
| 129 | +Pre-declare your TS type, get compile-time errors when your definition doesn't match. |
| 130 | + |
| 131 | +Now supports morph side declarations and optionality via property values. |
| 132 | + |
| 133 | +```ts |
| 134 | +// type.declare<{ a: string }>().type({ a: "1" }) |
| 135 | +// TS Error: declared: string; inferred: 1 |
| 136 | +``` |
| 137 | + |
| 138 | +--- |
| 139 | + |
| 140 | +**9/** |
| 141 | + |
| 142 | +More: |
| 143 | + |
| 144 | +- type.or, type.and, type.merge, type.pipe - n-ary standalone operators |
| 145 | +- "|>" string-embeddable pipe syntax |
| 146 | +- type.valueOf for TS enums |
| 147 | +- string.hex + string.regex keywords |
| 148 | +- Serializable ArkErrors (flatByPath, flatProblemsByPath, toJSON) |
| 149 | +- TraversalError replaces AggregateError |
| 150 | +- exactOptionalPropertyTypes config |
| 151 | +- ES2020 + Hermes compatibility |
| 152 | +- In-docs playground |
| 153 | +- Better JSDoc + go-to-definition |
| 154 | + |
| 155 | +--- |
| 156 | + |
| 157 | +**10/** |
| 158 | + |
| 159 | +Full announcement: arktype.io/docs/blog/2.2 |
| 160 | + |
| 161 | +I couldn't be more hyped to see what you do with it. |
| 162 | + |
| 163 | +⚡ arktype.io/docs/intro/setup |
| 164 | +⭐ github.com/arktypeio/arktype |
| 165 | +👋 arktype.io/discord |
| 166 | + |
| 167 | +If you want to support this work: github.com/sponsors/arktypeio |
0 commit comments