react-component / form

React High Order Form Component(web & react-native)
http://react-component.github.io/form/
MIT License
1.81k stars 295 forks source link

increased type safety via string literals #496

Open dgreene1 opened 3 years ago

dgreene1 commented 3 years ago

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 mispell 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;
dgreene1 commented 3 years ago

If approved, I will submit this PR.