fix(RadioGroup): improve type safety for normalizeItem function (#4535)

Co-authored-by: Sandro Circi <sandro.circi@digitoolmedia.com>
This commit is contained in:
kyyy
2025-07-17 02:58:05 +07:00
committed by GitHub
parent c64c4cdea0
commit bb99345f5b

View File

@@ -70,7 +70,9 @@ export type RadioGroupEmits = RadioGroupRootEmits & {
change: [payload: Event] change: [payload: Event]
} }
type SlotProps<T extends RadioGroupItem> = (props: { item: T & { id: string }, modelValue?: RadioGroupValue }) => any type NormalizeItem<T extends RadioGroupItem> = Exclude<T & { id: string }, RadioGroupValue>
type SlotProps<T extends RadioGroupItem> = (props: { item: NormalizeItem<T>, modelValue?: RadioGroupValue }) => any
export interface RadioGroupSlots<T extends RadioGroupItem = RadioGroupItem> { export interface RadioGroupSlots<T extends RadioGroupItem = RadioGroupItem> {
legend(props?: {}): any legend(props?: {}): any
@@ -114,21 +116,21 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.radioGroup |
indicator: props.indicator indicator: props.indicator
})) }))
function normalizeItem(item: any) { function normalizeItem(item: T): NormalizeItem<T> {
if (item === null) { if (item === null) {
return { return {
id: `${id}:null`, id: `${id}:null`,
value: undefined, value: undefined,
label: undefined label: undefined
} } as NormalizeItem<T>
} }
if (typeof item === 'string' || typeof item === 'number') { if (typeof item === 'string' || typeof item === 'number' || typeof item === 'bigint') {
return { return {
id: `${id}:${item}`, id: `${id}:${item}`,
value: String(item), value: String(item),
label: String(item) label: String(item)
} } as NormalizeItem<T>
} }
const value = get(item, props.valueKey as string) const value = get(item, props.valueKey as string)
@@ -136,7 +138,7 @@ function normalizeItem(item: any) {
const description = get(item, props.descriptionKey as string) const description = get(item, props.descriptionKey as string)
return { return {
...item, ...(item as NormalizeItem<T>),
value, value,
label, label,
description, description,