#3897·headlessui

当 `TValue` 是联合类型时,Combobox `by` 和 `virtual.disabled` 会拒绝正确的处理程序

作者: dimazuien创建于 2026年9月28日更新于 2026年9月28日

ComboboxProps derives by, virtual.options and virtual.disabled from EnsureArray<NoInfer<TValue>>. NoInfer blocks the conditional inside EnsureArray from distributing, so when TValue is a union the Expand branch maps over the union as a whole and yields only the members its constituents have in common. For TValue = string | Option that is { toString; valueOf } instead of string | Option.

The two helpers, verbatim from dist/types.d.ts:

typescript
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never;
type EnsureArray<T> = T extends any[] ? T : Expand<T>[];

Both conditionals distribute over a naked type parameter, but NoInfer<T> is a substitution type, so EnsureArray<NoInfer<T>> does not distribute:

内容来源: tailwindlabs/headlessui