Combobox `by` and `virtual.disabled` reject a correct handler when `TValue` is a union
What package within Headless UI are you using?
@headlessui/react
What version of that package are you using?
v2.2.10
What browser are you using?
N/A — this is a type-checking issue.
Reproduction URL
Self-contained TypeScript Playground (no Headless UI install needed — the two type helpers are copied verbatim from dist/types.d.ts):
Describe your issue
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:
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:
type Option = { label: string; value: string; disabled?: boolean };
type Value = string | Option;
type WithoutNoInfer = EnsureArray<Value>[number];
const ok: WithoutNoInfer = { label: 'a', value: 'a' }; // OK
type WithNoInfer = EnsureArray<NoInfer<Value>>[number];
const bad: WithNoInfer = { label: 'a', value: 'a' };
// Error TS2353: Object literal may only specify known properties, and 'label'
// does not exist in type '{ toString: (() => string) | (() => string);
// valueOf: (() => string) | (() => Object); }'Because TMultiple extends true ? ... : ... distributes when multiple is a plain boolean (as it is for any reusable wrapper that forwards the prop), both branches land in the parameter type, and by is declared as:
by?: (
a: NoInfer<Value> | { toString: (() => string) | (() => string); valueOf: (() => string) | (() => Object) },
z: /* same */,
) => booleanSince #3727 made these contravariant, an honest comparator over the real value type no longer assigns. Against the real package:
type Option = { label: string; value: string; disabled?: boolean };
type Value = string | Option;
const getValue = (o: Value) => (typeof o === 'string' ? o : o.value);
const compare = (a: Value, z: Value) => getValue(a) === getValue(z);
declare const multiple: boolean;
declare const options: Option[];
<Combobox<Value, boolean>
multiple={multiple}
by={compare}
virtual={{
options,
disabled: (o: Value) => typeof o === 'object' && !!o.disabled,
}}
/>;gives, under strict:
error TS2322: Type '(a: Value, z: Value) => boolean' is not assignable to type
'(a: NoInfer<Value> | { toString: ...; valueOf: ... }, z: ...) => boolean'.
error TS2322: Type '(o: Value) => boolean' is not assignable to type
'(value: NoInfer<Value> | { toString: ...; valueOf: ... }) => boolean'.Expected: by and virtual.disabled should receive TValue (here string | Option). A union TValue is ordinary for a combobox — free-text entry alongside option objects is exactly the creatable pattern.
Workaround (for anyone who finds this): assert the two props to their declared types, e.g. by={compare as ComboboxProps<Value, boolean>['by']}. The runtime values are already correct; only the declared parameter type is wrong.
Possible fix: Expand exists to flatten intersections for readability, but it is destructive on a union. Two options, both checked against the repro above:
Drop
ExpandfromEnsureArray:type EnsureArray<T> = T extends any[] ? T : T[];Keep
Expand, but applyNoInferoutsideEnsureArrayat the use sites inComboboxProps, so the conditional still sees a naked type parameter:by?: ByComparator< TMultiple extends true ? NoInfer<EnsureArray<TValue>>[number] : NoInfer<TValue> >;
Note that wrapping the inner conditional (T extends unknown ? Expand<T> : never) does not work — NoInfer blocks that one from distributing too.
Source: tailwindlabs/headlessui