Closed nd0ut closed 1 month ago
Oh, got it.
const DynamicVariantSchema = <TEntries extends ObjectEntries, TMessage extends ErrorMessage<ObjectIssue> | undefined>(
resultSchema: ObjectSchema<TEntries, TMessage>,
) =>
v.variant('success', [
v.object({
success: v.literal(false),
error: v.instance(Error),
}),
v.object({
success: v.literal(true),
result: resultSchema,
}),
]);
Hey @fabian-hiller, I have another, a bit more complex example:
const extendBaseSchema = <
TEntries extends ObjectEntries,
TMessage extends ErrorMessage<ObjectIssue> | undefined
>(
schema: ObjectSchema<TEntries, TMessage>
) => {
return variant("error", [
object({
error: literal("not found"),
obj_type: literal("error"),
}),
object({
error: literal("not authenticated"),
obj_type: literal("error"),
login_link: string(),
}),
object({
error: undefined_(),
...schema.entries
}),
]);
};
I'm unable to extend one of the variants with the provided object schema entries. Could suggest a solution please?
This works if you don't care about the exact output type.
Hello!
Here is the playground MRE: https://valibot.dev/playground/?code=JYWwDg9gTgLgBAKjgQwM5wG5wGZQiOAcg2QBtgAjCGQgbgCh6BjCAO1XgCUBTVAV1IwAykwAW3EMjgBeTADoIFAFbcmMABQBvenDgBrbgE8AXPI5RgrAObqAlABp6AX1sNmbDnCExkMYEwA1ZAtkVmExCSlZDDkSELD1Qn4mJl5UQns4AG0deUUVNS1c3WTU1FRTGPIYbigydWwyVG4HYrhavChKuUsOUNT1AFEoTtbdF0ddGPzVDW1dEr4UtO7q2vqYKD4WyYWoXgEYUx5+QRFxSV2J+gBdV0YWdngAEUNWZBB-IPjwi6i4AA8ABV2gAPGqsAAm6BiAHllLNzpEAaFDJlUQA+DHqXL7U6-SKmIGOWwyDG5GJxYChDRJJZldKZHILPIIwrzFlwUoreRrOqkBpNHZtXQdaDdXo+VgDYajXbjMYLaZsuYirn0nlVYA1fnqTbbRUsvGHUzGs4RS5ta66O4MIA
Inline code:
At runtime, it's working OK, but I got an error from TS:
Am I doing something wrong with the generics, or is it a bug?