Kevin Powell - CommunityKP-C
Kevin Powell - Community8mo ago
77 replies
Ganesh

typescript simplify a type guard

const validationResult = UserLoginSchema.safeParse(req.body);

    if (
        validationResult.error instanceof ZodError ||
        validationResult.data === undefined
    ) {
        res.status(400).json(validationResult.error);
        return;
    }

    const { name, password } = validationResult.data;


I have this code. I want the if statement to be able to verify that validationResullt.error is a ZodError and validation.data is not undefined but there doesn't seem a way to do them in one statement.

should I use as to explicitly assert the type of validation.data? I don't want another if statement because it would just duplicate the logic inside first if
Was this page helpful?