2

ArkType | ArkType

 1 year ago
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.
neoserver,ios ssh client
typescript.svg
vscode.svg
intellij.svg
vim.svg

ArkType

The first isomorphic type system for TS/JS

javascript.svg
chromium.svg
node.svg
deno.svg
[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
isomorphicHover.png

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"
})
arkUser.png
// 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()
})
zodInfer.png

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'"
}
}
)
optimizedUnion.png
// Hover to see internal representation...
export const numericIntersection = type(
"(1 <= number%2 < 100) & (0 < number%3 <= 99)"
)

optimizedNumber.png

Type-safe

String definitions are statically parsed with each character you type and give detailed feedback just like in your editor.

Worried about performance? Don't be. This is how it feels to interact with a scope of 100 cyclic types (you may want to go fullscreen to see the details!)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK