I have an API function that returns a user, for example. I want to parse response from the server to my client User type. Let's say e.g. I want to merge first and last name of a user into one string to use in my client code.
type User = {
uuid: string
email: string
fullName: string
}
const UserSchema: z.ZodSchema<User, z.ZodTypeDef, unknown> = z.object({
uuid: z.string().uuid(),
email: z.string().email(),
firstName: z.string(),
lastName: z.string()
}).transform(response => ({
uuid: response.uuid,
email: response.email,
fullName: `${response.firstName} ${response.lastName}`
}))
and that works perfectly fine from TypeScript perspective. E.g. if I try to set non string value to fullName inside transform it shows an error. BUT it allows me to add properties to the final result without showing any errors:
...}).transform(response => ({
uuid: response.uuid,
email: response.email,
fullName: `${response.firstName} ${response.lastName}`,
// here is an extra value that should not be here because it
// doesn't exist in my client User type but TS allows it here
blablabla: true,
}))
I have strict and strictNullChecks enabled in my tsconfig file. How can I make an error visible in such cases?