From c62b7b0882761502e7817ac42b4ef87243522abd Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Sat, 19 Oct 2019 16:57:26 +0200 Subject: [PATCH] safer types --- lib/index.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 0b31487..4329373 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -29,7 +29,11 @@ function isFormValidatorObject(validator: useFormValidatorMethod | useFormValida const defaultErrorMessage = (key: any) => `Error in ${key}` -export const useForm = (init: T, validators: Partial = {}, options: useFormOptions = {}) => { +export const useForm = ( + init: T, + validators: Partial = {}, + options: useFormOptions = {} +) => { const [form, setForm] = useState(init) const [errors, setErrors] = useState>({}) @@ -39,7 +43,7 @@ export const useForm = acc || cur !== undefined, false)) }, [errors]) - const _set = (key: keyof T, value: any) => { + const _set = (key: A, value: T[A]) => { setForm({ ...form, [key]: value, @@ -82,7 +86,7 @@ export const useForm = (value: any) => { + const update = (key: A, extractor = options.extractor) => (value: T[A]) => { const extracted = extractor ? extractor(value) : HTMLInputExtractor(value) _set(key, extracted) _validate(key, extracted) @@ -93,5 +97,5 @@ export const useForm =