Latest "keys:distilled" equivalent?
Hello, I started using ArkType a back in v1 and it has obviously come on leaps and bounds since then which is great to see.
However, back in v1 you could do something like
and once you validated a payload against this you would only ever get an object with
email
/password
keys.
I'm wondering how to achieve a similar result with the latest version of the library. Thanks.7 Replies
Two ways to do this:
The second is slightly more optimal perf-wise since it doesn't require creating two types.
Sorry, working on new docs for this now!
No problem at all, thank you for the (impressively fast) response! 👍
@ArkDavid If I add onUndeclaredKey("delete"), I can't further use this type to construct more types
const t = type({
a: "string"
}).onUndeclaredKey("delete")
const t2 = t.and({b:"string"})
@aabad_ankit use ```ts code ``` for code
There is
.merge
Great, any reason why and doesn't work but merge works.
Yes, because and means "satisfies both of these types". But if you add new keys to a type with
onUndeclaredKey: delete
, you haven't satisfied the original type, which would have deleted that key
Merge means "combine the property declarations of each of these types, giving priority to the right side", so it's fine
Kind of like how you can override previously declared props with ...
when you're merging objectsGot it. Thank you