fix(FormField): added a utility type to fix some type errors (#81)

Co-authored-by: Mauro Erta <mauro.erta@sap.com>
Co-authored-by: Benjamin Canac <canacb1@gmail.com>
This commit is contained in:
Mauro Erta
2024-04-29 18:12:09 +02:00
committed by GitHub
parent 46bd407b91
commit 559a8cba58
3 changed files with 9 additions and 6 deletions

View File

@@ -1,14 +1,13 @@
import { inject, ref, computed } from 'vue'
import { type UseEventBusReturn, useDebounceFn } from '@vueuse/core'
import type { FormEvent, FormInputEvents, FormFieldInjectedOptions, FormInjectedOptions } from '#ui/types/form'
import type { GetObjectField } from '#ui/types/utils'
type Props<T> = {
id?: string
name?: string
// @ts-expect-error FIXME: TS doesn't like this
size?: T['size']
// @ts-expect-error FIXME: TS doesn't like this
color?: T['color']
size?: GetObjectField<T, 'size'>
color?: GetObjectField<T, 'color'>
eagerValidation?: boolean
legend?: string
disabled?: boolean

View File

@@ -3,6 +3,7 @@ import type { ZodSchema } from 'zod'
import type { Schema as JoiSchema } from 'joi'
import type { ObjectSchema as YupObjectSchema } from 'yup'
import type { ObjectSchemaAsync as ValibotObjectSchema } from 'valibot'
import type { GetObjectField } from './utils'
export interface Form<T> {
validate (opts?: { name: string | string[], silent?: false, nested?: boolean }): Promise<T | false>
@@ -71,8 +72,7 @@ export interface FormInjectedOptions {
export interface FormFieldInjectedOptions<T> {
id: Ref<string | undefined>
name: ComputedRef<string | undefined>
// @ts-expect-error FIXME: TS doesn't like this
size: ComputedRef<T['size']>
size: ComputedRef<GetObjectField<T, 'size'>>
error: ComputedRef<string | boolean | undefined>
eagerValidation: ComputedRef<boolean | undefined>
validateOnInputDelay: ComputedRef<number | undefined>

View File

@@ -4,3 +4,7 @@ export type DeepPartial<T> = Partial<{
export type DynamicSlots<T extends { slot?: string }, SlotProps, Slot = T['slot']> =
Record<string, SlotProps> & (Slot extends string ? Record<Slot, SlotProps> : Record<string, never>)
export type GetObjectField<MaybeObject, Key extends string> = MaybeObject extends Record<string, any>
? MaybeObject[Key]
: never