A
arktypeโ€ข3w ago
PIat

Default number

Hello! Setting this brings the expected result, when ran through inferAmbient:
const queryType = type({
page: 'number',
})

// Result:
number
const queryType = type({
page: 'number',
})

// Result:
number
But adding a default value returns a function signature:
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
const queryType = type({
page: 'number = 1',
})

// Result
page?: ((In?: number | undefined) => Default<1>) | undefined
How can I get the number type in the editor when providing a default value?
86 Replies
TizzySaurus
TizzySaurusโ€ข3w ago
It's not clear to me what exactly you're after here, but maybe t["infer"] or t["tOut"] is what you're after? Where t in this case is your queryType
ssalbdivad
ssalbdivadโ€ข3w ago
Yeah you probably want t.infer for the output or t.inferIn for the input type (would allow undefined). tOut is more internal as it contains constraint brands
PIat
PIatโ€ข3w ago
Thank you!!! inferIn is exactly what I needed I am using Arktype as my form, params and query schema, and am inferring the types from the schema for type-safe endpoints
ssalbdivad
ssalbdivadโ€ข3w ago
If you want to create it from typed input, there's also a from method that accepts it Like queryType.from({}) Would result in {page: 1}
PIat
PIatโ€ข3w ago
And if I do
queryType.from({
page: 20
})
queryType.from({
page: 20
})
the result will be {page: 20}?
ssalbdivad
ssalbdivadโ€ข3w ago
Yeah it should
PIat
PIatโ€ข3w ago
So for form parsing it's a better variant than doing
queryType({ page: 20 })
queryType({ page: 20 })
?
ssalbdivad
ssalbdivadโ€ข3w ago
If you're ever passing in known data like that, I'd use from so you get type safety The default method is for accepting unknown
PIat
PIatโ€ข3w ago
Actually it was infer, for the output as you said ๐Ÿ˜… But other than that, they both produce the same result on runtime?
ssalbdivad
ssalbdivadโ€ข3w ago
Yes, under the hood it just calls assert which is also identical to the base method other than that if there are errors, it will throw instead of returning an errors object But it looks like in a case like that where you're passing an object literal there should never be errors
PIat
PIatโ€ข3w ago
There will be errors if someone passes a wrong type
ssalbdivad
ssalbdivadโ€ข3w ago
So in your case queryType({page: 20}) is just an exam,ple
PIat
PIatโ€ข3w ago
If they send
queryType({ page: "some string" })
queryType({ page: "some string" })
ssalbdivad
ssalbdivadโ€ข3w ago
But doesn't TS enforce that anyways? or would they not really be passing an object literal It seems like if they passed something invalid you would want to throw
PIat
PIatโ€ข3w ago
They're always passing an object literal (Remix converts formData and query params into an object), but they might be passing wrong keys or types if they'll call it through some other client than the website
Want results from more Discord servers?
Add your server