A
arktype•3mo ago
PIat

Extracting defaults at runtime

Hello! What is the canon way to extract the default values of the type on runtime? I'd like to uuse the defaults from the type as defaults in a form.
const formDef = type({
cars: '5 < number < 100 = 50',
name: ['string[]', '=', []],
active: 'boolean = true',
})

<Form
defaultValue={
// ...defaults from formDef
}
>
const formDef = type({
cars: '5 < number < 100 = 50',
name: ['string[]', '=', []],
active: 'boolean = true',
})

<Form
defaultValue={
// ...defaults from formDef
}
>
2 Replies
ssalbdivad
ssalbdivad•3mo ago
Well there's the new .map API but that's really more for going types => types. I guess you could map to a type with units as the defaults, then extract out those units https://github.com/arktypeio/arktype/blob/d544f780498adab1e10eb4690f913a4f0911f8da/ark/type/__tests__/objects/mapped.test.ts Note TS is super finicky about how it infers some of these return types so you might have to annotate some of them if you need type safety, but that was always true in the past anyways at least now the logic is simpler 😛
PIat
PIatOP•3mo ago
Thank you! I'll try this out!
Want results from more Discord servers?
Add your server