My question pertains to a specific scenario:
I have defined a type for form values
interface FormValues {
name: string
description: string
expirationDate: Date | null
}
and a type for potential errors that may occur in the form
const errors: Record<keyof FormValues, string | undefined> = {
name: undefined,
description: undefined,
expirationDate: undefined
}
However, the errors type only works when each field is explicitly defined as undefined. If I attempt to change it to an empty object, TypeScript throws an error
const errors: Record<keyof FormValues, string | undefined> = {}
/*
Type '{}' is missing the following properties
from type 'Record<keyof FormValues, string | undefined>': name, description, expirationDate
*/
How can I modify the error type to match my requirement of having an object with fields either defined as a string or non-existent:
const validate = values => {
const errors: ??? = {}
if (!values.name) errors.name = 'Required'
if (!values.description) errors.description = 'Required'
if (!values.expirationDate) errors.expirationDate = 'Required'
return errors
}