Skip to content

Commit 27c9ef2

Browse files
committed
feat(types): stronger define component bind types closes #4421
1 parent 804ec6f commit 27c9ef2

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

.changeset/orange-planes-grab.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
feat(types): stronger define component bind types closes #4421

packages/vee-validate/src/types/forms.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,11 +265,21 @@ export interface PrivateFormContext<TValues extends GenericObject = GenericObjec
265265
isFieldValid<TPath extends Path<TValues>>(path: TPath): boolean;
266266
}
267267

268-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
269-
export interface BaseComponentBinds<TValue = unknown, TModel = 'modelValue'> {
268+
interface ComponentModellessBinds {
270269
onBlur: () => void;
271270
}
272271

272+
type ComponentModelBinds<TValue = any, TModel extends string = 'modelValue'> = ComponentModellessBinds & {
273+
[TKey in `onUpdate:${TModel}`]: (value: TValue) => void;
274+
};
275+
276+
export type BaseComponentBinds<TValue = any, TModel extends string = 'modelValue'> = ComponentModelBinds<
277+
TValue,
278+
TModel
279+
> & {
280+
[k in TModel]: TValue;
281+
};
282+
273283
export type PublicPathState<TValue = unknown> = Omit<
274284
PathState<TValue>,
275285
'bails' | 'label' | 'multiple' | 'fieldsCount' | 'validate' | 'id' | 'type' | '__flags'
@@ -339,11 +349,12 @@ export interface FormContext<TValues extends GenericObject = GenericObject, TOut
339349
defineComponentBinds<
340350
TPath extends Path<TValues>,
341351
TValue = PathValue<TValues, TPath>,
352+
TModel extends string = 'modelValue',
342353
TExtras extends GenericObject = GenericObject,
343354
>(
344355
path: MaybeRefOrGetter<TPath>,
345356
config?: Partial<ComponentBindsConfig<TValue, TExtras>> | LazyComponentBindsConfig<TValue, TExtras>,
346-
): Ref<BaseComponentBinds<TValue> & TExtras>;
357+
): Ref<BaseComponentBinds<TValue, TModel> & TExtras>;
347358
defineInputBinds<
348359
TPath extends Path<TValues>,
349360
TValue = PathValue<TValues, TPath>,

packages/vee-validate/src/useForm.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ export function useForm<
959959
function defineComponentBinds<
960960
TPath extends Path<TValues>,
961961
TValue = PathValue<TValues, TPath>,
962+
TModel extends string = 'modelValue',
962963
TExtras extends GenericObject = GenericObject,
963964
>(
964965
path: MaybeRefOrGetter<TPath>,
@@ -990,7 +991,7 @@ export function useForm<
990991
[model]: pathState.value,
991992
[`onUpdate:${model}`]: onUpdateModelValue,
992993
...(configVal.props || {}),
993-
} as BaseComponentBinds<TValue> & TExtras;
994+
} as BaseComponentBinds<TValue, TModel> & TExtras;
994995
}
995996

996997
const model = config?.model || 'modelValue';
@@ -1004,10 +1005,10 @@ export function useForm<
10041005
return {
10051006
...base,
10061007
...config.mapProps(omit(pathState, PRIVATE_PATH_STATE_KEYS)),
1007-
} as BaseComponentBinds<TValue> & TExtras;
1008+
} as BaseComponentBinds<TValue, TModel> & TExtras;
10081009
}
10091010

1010-
return base as BaseComponentBinds<TValue> & TExtras;
1011+
return base as BaseComponentBinds<TValue, TModel> & TExtras;
10111012
});
10121013

10131014
return props;

0 commit comments

Comments
 (0)