A
arktype•2mo ago
chris777

Is there an any type?

Hi there, I wanted to use an arktype for some Typescript, eg:
type FormData = {
id: string
meta: any
}
type FormData = {
id: string
meta: any
}
The closest I can see is perhaps:
const FormDataType = type({
id: 'string',
meta: type('object').as<any>(),
})
const FormDataType = type({
id: 'string',
meta: type('object').as<any>(),
})
I was previously using zod and was suprised not to see it in arktype eg z.any(); // inferred type: 'any' https://zod.dev/api?id=unknown Interested if I am missing support for any somewhere in arktype, it would be much nicer to write:
const FormDataType = type({
id: 'string',
meta: 'any',
})
const FormDataType = type({
id: 'string',
meta: 'any',
})
4 Replies
TizzySaurus
TizzySaurus•2mo ago
There's "unknown" which is basically just a type-safe version of any
chris777
chris777OP•2mo ago
thanks @TizzySaurus yes indeed there is unknown its not a 1:1 with any though thats why zod includes both eg
// allows any values
z.any(); // inferred type: `any`
z.unknown(); // inferred type: `unknown`
// allows any values
z.any(); // inferred type: `any`
z.unknown(); // inferred type: `unknown`
ssalbdivad
ssalbdivad•2mo ago
The builtin keyword here is unknown.any. It will behave the same as unknown at runtime but is inferred as any i.e. type({ meta: "unknown.any" }) At some point any was available as a top-level keyword, but generally unknown is the better option in almost any scenario like this because it forces you to check the type of data before you interact with it. Depending on what you know about the shape of your data, you could also consider something like type({ meta: "Record<string, unknown>" }) if you want to allow arbitrary property access for an object without losing safety.
chris777
chris777OP•2mo ago
@ssalbdivad ah nice! unknown.any is much closer to the Typescript. Indeed unknown is generally a better option to force the checking. In this case I wasn't looking to work on that change just yet. It's great to be able to write so closely to any existing Typescript type like this. Coming from zod the 'type' syntax with all what Arktype provides grows on you, thanks again 😉

Did you find this page helpful?