ArkType | ArkType
source link: https://arktype.io/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
ArkType
The first isomorphic type system for TS/JS
[email protected]
in VS Code— no extensions or plugins required (how?)npm install arktype
ArkType is a library for defining runtime types using TypeScript syntax that can be inferred 1:1.
Each character you type is instantly validated both syntactically and semantically using TypeScript's own type system, so you know exactly what to expect from editor to runtime ⛵
Isomorphic
Define types using TS syntax. Infer them 1:1. Use them to validate your data at runtime.
const user = type({
name: "string",
device: {
platform: "'android'|'ios'",
"version?": "number"
}
})
// Hover to infer...
type User = typeof user.infer
Concise
Say more with less
// Hover to infer...
const arkUser = type({
name: /^ark.*$/ as Infer<`ark${string}`>,
birthday: morph("string", (s) => new Date(s)),
"powerLevel?": "1<=number<9000"
})
// Hover to infer...
const zodUser = z.object({
name: z.custom<`zod${string}`>(
(val) => typeof val === "string" && /^zod.*$/.test(val)
),
birthday: z.preprocess(
(arg) => (typeof arg === "string" ? new Date(arg) : undefined),
z.date()
),
powerLevel: z.number().gte(1).lt(9000).optional()
})
Optimized
ArkType is not just a validator— it's a full type system. Operations are deeply computed and optimized by default
// Hover to see internal representation...
export const deepLeftOrRight = union(
{
auto: {
discriminated: "'left'"
}
},
{
auto: {
discriminated: "'right'"
}
}
)
// Hover to see internal representation...
export const numericIntersection = type(
"(1 <= number%2 < 100) & (0 < number%3 <= 99)"
)
Type-safe
String definitions are statically parsed with each character you type and give detailed feedback just like in your editor.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK