field-form icon indicating copy to clipboard operation
field-form copied to clipboard

increased type safety via string literals

Open dgreene1 opened this issue 4 years ago • 0 comments

We would get considerably better type safety out of many of the form instance functions if we used the keyof operator. For instance, this simple change would make it impossible to accidentally misspell the field that we're looking for.

export interface FormInstance<Values = any> {
    getFieldValue: (name: NamePath) => StoreValue;

to this:

export interface FormInstance<Values = any> {
    getFieldValue: <NamePath extends keyof FormFieldsAndValues>(name: NamePath) => FormFieldsAndValues[NamePath] | undefined;

So then if field you wanted to get was called "address" you couldn't make the mistake of typing:

form.getFieldValue("addr")

dgreene1 avatar Feb 11 '21 04:02 dgreene1