From f815fc67e39147c478b7df616928ed14dcafd8ff Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Fri, 23 Jun 2023 21:57:41 +0800 Subject: [PATCH] fix(core): fix `register` type error (#28) --- .../core/src/composables/useFieldArray.ts | 2 +- packages/core/src/composables/useForm.ts | 18 ++++++++--- .../src/composables/useInternalContext.ts | 32 ++++++++++++------- packages/core/src/types.ts | 6 ++-- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/packages/core/src/composables/useFieldArray.ts b/packages/core/src/composables/useFieldArray.ts index d19fa31..be594e5 100644 --- a/packages/core/src/composables/useFieldArray.ts +++ b/packages/core/src/composables/useFieldArray.ts @@ -102,7 +102,7 @@ export function useFieldArray( const fields: Ref[]> = ref([]); const values = computed(() => { - return getFieldValue(name).value || []; + return getFieldValue(name).value || []; }); let seed = 0; diff --git a/packages/core/src/composables/useForm.ts b/packages/core/src/composables/useForm.ts index 2a412ca..529b746 100644 --- a/packages/core/src/composables/useForm.ts +++ b/packages/core/src/composables/useForm.ts @@ -15,8 +15,10 @@ import keysOf from '../utils/keysOf'; import set from '../utils/set'; import type { Reducer } from './useFormStore'; +import type { InternalContextValues } from './useInternalContext'; import type { FieldAttrs, + FieldError, FieldMeta, FormErrors, FormEventHandler, @@ -406,9 +408,12 @@ export function useForm< }); }; - const getFieldMeta = >( + const getFieldMeta = < + Name extends Path, + Value = PathValue, + >( name: MaybeRefOrGetter, - ): FieldMeta> => { + ): FieldMeta => { const error = computed(() => getFieldError(name)); const touched = computed(() => getFieldTouched(name)); const dirty = computed(() => getFieldDirty(name)); @@ -429,7 +434,12 @@ export function useForm< })); }; - const getFieldError = (name: MaybeRefOrGetter): FormErrors => { + const getFieldError = < + Name extends Path, + Value = PathValue, + >( + name: MaybeRefOrGetter, + ): FieldError => { name = toValue(name); return get(state.errors.value, name); }; @@ -628,7 +638,7 @@ export function useForm< validateField, }; - provide(InternalContextKey, { + provide>(InternalContextKey, { getFieldValue, setFieldValue, getFieldError, diff --git a/packages/core/src/composables/useInternalContext.ts b/packages/core/src/composables/useInternalContext.ts index 9365489..85512f1 100644 --- a/packages/core/src/composables/useInternalContext.ts +++ b/packages/core/src/composables/useInternalContext.ts @@ -9,10 +9,12 @@ import { import { FieldArrayValidator, FieldAttrs, - FormErrors, + FieldError, FormTouched, FormValues, MaybeRefOrGetter, + Path, + PathValue, SetFieldArrayValue, UseFormRegister, UseFormSetFieldValue, @@ -26,21 +28,25 @@ function injectMaybeSelf( return vm?.provides[key as any] || inject(key, defaultValue); } -export interface InternalContextValues { +export interface InternalContextValues { registerFieldArray: ( - name: MaybeRefOrGetter, + name: MaybeRefOrGetter>, options: { - validate?: FieldArrayValidator; + validate?: FieldArrayValidator>>; reset: () => void; }, ) => void; - getFieldValue: ( - name: MaybeRefOrGetter, - ) => WritableComputedRef; + getFieldValue: ( + name: MaybeRefOrGetter>, + ) => WritableComputedRef>>; + setFieldValue: UseFormSetFieldValue; - getFieldError: (name: MaybeRefOrGetter) => FormErrors; + getFieldError: ( + name: MaybeRefOrGetter>, + ) => FieldError>>; + getFieldTouched: (name: MaybeRefOrGetter) => FormTouched; getFieldDirty: (name: MaybeRefOrGetter) => boolean; getFieldAttrs: (name: MaybeRefOrGetter) => ComputedRef; @@ -50,11 +56,13 @@ export interface InternalContextValues { register: UseFormRegister; } -export const InternalContextKey: InjectionKey = Symbol( - __DEV__ ? 'vorms internal context' : '', -); +export const InternalContextKey: InjectionKey< + InternalContextValues +> = Symbol(__DEV__ ? 'vorms internal context' : ''); export function useInternalContext() { - const context = injectMaybeSelf(InternalContextKey) as InternalContextValues; + const context = injectMaybeSelf( + InternalContextKey, + ) as InternalContextValues; return context; } diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index ed4e4db..ed62175 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -35,8 +35,8 @@ export type FormErrors = { }; export type FieldError = Value extends Primitive - ? string | string[] | void - : string | string[] | FormErrors | void; + ? string | string[] | undefined + : string | string[] | FormErrors | undefined; export interface FormState { values: Values; @@ -154,7 +154,7 @@ export type FieldAttrs = { export type FieldMeta = { dirty: ComputedRef; - error: ComputedRef>; + error: ComputedRef>; touched: ComputedRef; };