You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Contravariance is a property associated of generic types on their type parameters where the generic type G<A> extends G<B> if B extends A. It is usually associated with type parameters used as inputs.
Each ZodType is composed of an input and an output type. Consider, for instance, the following Zod schema:
consts=z.object({a: z.string().default("foo")});
Its input type has a as an optional field, but it is required on the output type:
Given that the input and output types are type parameters to ZodType, one might expect ZodType to be covariant on the output type and contravariant on the input type. While the former is true, the latter, unfortunately, is not.
Example
functionf<Sextendsz.ZodType<string,any,any>>(_: S){}f(z.literal("a"));// Works finefunctiong<Sextendsz.ZodType<any,any,"a">>(_: S){}g(z.string());// Errors out :(
Expected behaviour
ZodType should be contravariant with respect to the input type
Actual behaviour
ZodType is not contravariant with respect to the input type
The text was updated successfully, but these errors were encountered:
Background
Contravariance is a property associated of generic types on their type parameters where the generic type
G<A>
extendsG<B>
ifB
extendsA
. It is usually associated with type parameters used as inputs.Each
ZodType
is composed of an input and an output type. Consider, for instance, the following Zod schema:Its input type has
a
as an optional field, but it is required on the output type:Description
Given that the input and output types are type parameters to
ZodType
, one might expectZodType
to be covariant on the output type and contravariant on the input type. While the former is true, the latter, unfortunately, is not.Example
Expected behaviour
ZodType
should be contravariant with respect to the input typeActual behaviour
ZodType
is not contravariant with respect to the input typeThe text was updated successfully, but these errors were encountered: