Syntax Overview

Someone was asking for an overview of available syntax. I ended up coming this and figured it would be useful to post it here. It doesn't include everything (notably most tuple expressions and scopes), but it's a good place to start:
import { type } from "arktype"

export const currentTsSyntax = type({
keyword: "null",
stringLiteral: "'TS'",
numberLiteral: "5",
bigintLiteral: "5n",
union: "string|number",
intersection: "boolean&true",
array: "Date[]",
grouping: "(0|1)[]",
objectLiteral: {
nested: "string",
"optional?": "number"
},
tuple: ["number", "number"]
})

// these features will be available in the upcoming release

export const upcomingTsSyntax = type({
keyof: "keyof bigint",
thisKeyword: "this", // recurses to the root of the current type
variadicTuples: ["true", "...false[]"]
})

export const validationSyntax = type({
keywords: "email|uuid|creditCard|integer", // and many more
builtinParsers: "parsedDate", // parses a Date from a string
nativeRegexLiteral: /@arktype\.io/,
embeddedRegexLiteral: "email&/@arktype\\.io/",
divisibility: "number%10", // a multiple of 10
bound: "alpha>10", // an alpha-only string with more than 10 characters
range: "1<=email[]<100", // a list of 1 to 99 emails
narrows: ["number", "=>", (n) => n % 2 === 1], // an odd integer
morphs: ["string", "|>", parseFloat] // validates a string input then parses it to a number
})

// in the upcoming release, you can use chaining to define expressions directly
// that use objects or functions that can't be embedded in strings

export const parseBigintLiteral = type({ value: "string" })
.and({
format: "'bigint'"
})
.narrow((data): data is { value: `${string}n`; format: "bigint" } =>
data.value.endsWith("n")
)
.morph((data) => BigInt(data.value.slice(-1)))

// data inferred as bigint | undefined
export const { data, problems } = parseBigintLiteral({value: "999n", format: "bigint"})
import { type } from "arktype"

export const currentTsSyntax = type({
keyword: "null",
stringLiteral: "'TS'",
numberLiteral: "5",
bigintLiteral: "5n",
union: "string|number",
intersection: "boolean&true",
array: "Date[]",
grouping: "(0|1)[]",
objectLiteral: {
nested: "string",
"optional?": "number"
},
tuple: ["number", "number"]
})

// these features will be available in the upcoming release

export const upcomingTsSyntax = type({
keyof: "keyof bigint",
thisKeyword: "this", // recurses to the root of the current type
variadicTuples: ["true", "...false[]"]
})

export const validationSyntax = type({
keywords: "email|uuid|creditCard|integer", // and many more
builtinParsers: "parsedDate", // parses a Date from a string
nativeRegexLiteral: /@arktype\.io/,
embeddedRegexLiteral: "email&/@arktype\\.io/",
divisibility: "number%10", // a multiple of 10
bound: "alpha>10", // an alpha-only string with more than 10 characters
range: "1<=email[]<100", // a list of 1 to 99 emails
narrows: ["number", "=>", (n) => n % 2 === 1], // an odd integer
morphs: ["string", "|>", parseFloat] // validates a string input then parses it to a number
})

// in the upcoming release, you can use chaining to define expressions directly
// that use objects or functions that can't be embedded in strings

export const parseBigintLiteral = type({ value: "string" })
.and({
format: "'bigint'"
})
.narrow((data): data is { value: `${string}n`; format: "bigint" } =>
data.value.endsWith("n")
)
.morph((data) => BigInt(data.value.slice(-1)))

// data inferred as bigint | undefined
export const { data, problems } = parseBigintLiteral({value: "999n", format: "bigint"})
No description
No description
No description
No description
12 Replies
Dimava
Dimava2y ago
I'm now asking for overview of unawailable syntax (i'm too sleepy to lol, so) huh
ssalbdivad
ssalbdivadOP2y ago
Sorry can't answer that without this https://github.com/arktypeio/arktype/issues/815
GitHub
Negated types · Issue #815 · arktypeio/arktype
This would allow some or all types to be negated (likely using a new ! operator). The challenge here would be integrating these checks with the type system so that intersections can still be reduce...
Dimava
Dimava2y ago
I mean more like not-in-0.14 / not-inplemented / not-planned
ssalbdivad
ssalbdivadOP2y ago
My response was a joke but the best thing to do is look at the unit tests. The organization isn't perfect but it's good. Anything that's commented out don't count on everything else is planned
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Dimava
Dimava14mo ago
@jakub yes (why didn't you just try it)
Unknown User
Unknown User14mo ago
Message Not Public
Sign In & Join Server To View
Dimava
Dimava14mo ago
(Recently someone asked (another) question with the why didn't you try reason being afk)
ssalbdivad
ssalbdivadOP14mo ago
Yes you can include arbitrary spaces between operators/operands
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
ssalbdivad
ssalbdivadOP11mo ago
Yeah you can do ["instanceof", File] Or instanceOf(File) with the helper method
Unknown User
Unknown User11mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server