extending a ZodType #1870
-
Hi, I have the following types Address and Location. Location extends Address. I'm trying to get zod schemas that are typed by the TS types. I have to resort to the solution below because import { z } from 'zod';
export interface Address {
id?: string;
addressText: string;
line1: string;
line2?: string;
city: string;
state: string;
postalCode: string;
country: string;
}
export interface Location extends Address {
latitude: number;
longitude: number;
businessName?: string;
}
const addressSchema_ = z.object({
id: z.string().optional(),
addressText: z.string(),
line1: z.string(),
line2: z.string().optional(),
city: z.string(),
state: z.string(),
postalCode: z.string(),
country: z.string(),
});
export const addressSchema: z.ZodType<Address> = addressSchema_;
export const locationSchema: z.ZodType<Location> = addressSchema_.extend({
latitude: z.number(),
longitude: z.number(),
businessName: z.string().optional(),
}); |
Beta Was this translation helpful? Give feedback.
Answered by
JacobWeisenburger
Jan 12, 2023
Replies: 1 comment 6 replies
-
Is this what you are looking for? interface Address {
id?: string
addressText: string
line1: string
line2?: string
city: string
state: string
postalCode: string
country: string
}
interface Location extends Address {
latitude: number
longitude: number
businessName?: string
}
const addressSchema = z.object( {
id: z.string().optional(),
addressText: z.string(),
line1: z.string(),
line2: z.string().optional(),
city: z.string(),
state: z.string(),
postalCode: z.string(),
country: z.string(),
} ) satisfies z.ZodType<Address>
const locationSchema = addressSchema.extend( {
latitude: z.number(),
longitude: z.number(),
businessName: z.string().optional(),
} ) satisfies z.ZodType<Location> |
Beta Was this translation helpful? Give feedback.
6 replies
Answer selected by
JacobWeisenburger
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this what you are looking for?