Hi, I'm new to TypeScript and am trying to wrap my head around something 🤪
I'm struggling to get TS to auto-complete on some type specific properties, such as species of Animal and taste of Food.
I don't want to add a property to my data objects as a discriminated union, if possible, and I want to avoid type assertions.
I have the following types:
import { z } from 'zod'
export interface FormStep<T extends keyof AllFormData> {
stepName: T
onSubmit: (stepName: T, data: AllFormData[T]) => void
}
export type AllFormData = {
animal: Animal
food: Food
}
export const animalSchema = z.object({
name: z.string(),
age: z.number().min(1, 'Age must be at least 1'),
species: z.string().min(1, 'You must select a species'),
})
export type Animal = z.infer<typeof animalSchema>
export const foodSchema = z.object({
name: z.string(),
amount: z.number().min(1, 'Amount must be at least 1'),
taste: z.string().min(1, 'You must select a taste score'),
})
export type Food = z.infer<typeof foodSchema>