Is there an any type?
Hi there,
I wanted to use an arktype for some Typescript, eg:
The closest I can see is perhaps:
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:
4 Replies
There's
"unknown"
which is basically just a type-safe version of any
thanks @TizzySaurus yes indeed there is unknown its not a 1:1 with
any
though thats why zod includes both eg
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.@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 😉