I have this function useFormState() that takes object initialValues of type FormType as an argument.
type FormType = {
email: string;
password: string;
rememberMe: boolean;
}
...
const initialValues: FormType = {
email: '',
password: '',
rememberMe: false,
}
const { values, touched, errors, updateField } = useFormState<FormType, keyof FormType>(initialValues);
Function useFormState() must return objects containing keys from FormType:
touched: {
email: true,
password: false,
rememberMe: false
}
In order to be able to type the response like this I need to extract "keys" type, so I pass it as second generic type keyof FormType.
And this is what my question is about - Is there any way to pass just one type FormType and extract keys type internally?
My function is defined like this:
const useFormer = <T, K extends keyof T>(props) => {
...
}
I could completely omit passing types and let the TS to infer the types and it kinda works but
- When I add more properties using
TTS gets confused and infers it wrong - I want function's user to be sure what they are passing is matching the Type they have so I think I want that one generic type.
It feels like the second one can be completely inferred K extends keyof T but if I pass just one type argument - TS wants second one.
Is there any way to get away with just one?