Object schemas with values of any or unknown #857
Replies: 3 comments 3 replies
-
I think I can answer my own question by inferring the type... const schema = object({ foo: unknown() })
type Schema = InferOutput<typeof schema>
const a: Schema = {}
// Error! Looks like a bug to me. |
Beta Was this translation helpful? Give feedback.
-
The |
Beta Was this translation helpful? Give feedback.
-
Ah, OK. I guess my biggest struggle here is that the type inferring is different to the runtype checking. Would it be at all possible to align the returned type with this intention? Something like: const schema = object({ foo: unknown() })
type Schema = InferOutput<typeof schema>
// { foo?: unknown } This way there is much less confusion and the only potential increase in bundle size is in types, which will eventually be stripped out. |
Beta Was this translation helpful? Give feedback.
-
I just came across something which surprised me and I wanted to enquire if this is expected or a bug. When using an
object
,looseObject
orstrictObject
schema where values areunknown()
orany()
, trying to parse an object without those fields will pass. For example:However, if I were to change
foo
's schema to something else, it will not...Is anyone able to confirm whether I've found a bug or I'm missing a point?
Beta Was this translation helpful? Give feedback.
All reactions