I have a question around generic type for array input. I have a function called useForm and it accepts the following options
export interface DataType {
[key: string]: FieldValue;
}
export type FieldValue = boolean | string | string[] | number | {};
export default function useForm<Data extends DataType>(
{ mode, validationSchema, defaultValues, validationFields }: Props<Data> = {
mode: 'onSubmit',
defaultValues: {},
},
): UseFormFunctions<Data> {
}
export interface Props<Data> {
mode: 'onSubmit' | 'onBlur' | 'onChange';
defaultValues?: { [key: string]: any };
validationFields?: string[]; // how do i use generic type here to only allow key from FormData
validationSchema?: any;
}
type FormData = {
firstName: string,
lastName: string,
}
const { register } = useForm<FormData>({
validationFields: ['test'] // should throw error because it's not firstname or lastname
})
i want it to throw an type error when FormData type is not found.
useForm