A
arktype3d ago
PIat

Dynamic type based on another value

Given a theoretical example like this:
const formDef = type({
agree: 'boolean',
clauses: ['===', 'first', 'second'],
})
const formDef = type({
agree: 'boolean',
clauses: ['===', 'first', 'second'],
})
Is it possible to only have clauses be required if agree is true?
4 Replies
Dimava
Dimava3d ago
Or of a type with "true" And a type with false
PIat
PIat3d ago
AAAHHHH Narrowing 🫠 Thank you!
TizzySaurus
TizzySaurus3d ago
Not even that. Just use a discriminated union, like you would with raw TS.
// raw TS
type t = {agree: true, clauses: "first" | "second"} | {agree: false, clauses?: "first" | "second"

// arktype
const t = type({agree: true, clauses: "'first' | 'second'"}).or({agree: false, clauses?: "'first' | 'second'"})
// raw TS
type t = {agree: true, clauses: "first" | "second"} | {agree: false, clauses?: "first" | "second"

// arktype
const t = type({agree: true, clauses: "'first' | 'second'"}).or({agree: false, clauses?: "'first' | 'second'"})
PIat
PIat3d ago
Thank you 🙏🙏🙏
Want results from more Discord servers?
Add your server