#3897·headlessui

Combobox `by` and `virtual.disabled` reject a correct handler when `TValue` is a union

Author: dimazuienCreated Sep 28, 2026Updated Sep 28, 2026

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):

https://www.typescriptlang.org/play/?strictFunctionTypes=true&ts=5.9.2#code/PTAEGEHsAcEsFMAmoBu8BOAjAhgF1gLagBm6kRAAgBbzaIA28AzkwK6zDq0DGuoATADohARgAMoABSJYTXMFwBPaM0GJBuJgEoAUEpWgAogA9o2AHaIAPABUAfKAC8oG6HjHc8S01CxzxDFAAeVAAflAAb1AAbQBpX3NQAGt4RUhiYIBdAC5guMzQAF9QXPN4NHQAbj1leCNzNi4AQXR0bEVbB2dXd09vUAtFaILw11yTM0tO4eqagyDofEhE5yj6bEx4ely5dD8Ac0rUbHpWeB3cPfND0BkmDcZEUNzMSEhGCyLq-TqANROzk5QLsDqAAD7BRawZazECgADqsFwVEgrD4ADlIABJfwYAA09Ua8BabUUt1kl1gmDRzFAkAqoGRdVY5mhiQsyFkoG4kFa8F4gjmdURyNRuExOIC6CBhgarGarXaVn+p3gdmi5lYBE26Ey1R5DT4kCSIlyIpRaIluOlzgA5NhbUdQM6Xa63e7XXCgrEdAa5HSkvwzUiLeLsdagWsNltcvbbQSUADzqA419PWBvTodHD4W1oCpOYkrVKCe5sLx6GTsD4oNrIK9jAAFMjQHyISDMAlI4G4GBtil7an4a4DSzZsA8+jrVu03uMmigAjwbUYHwgkccyFLRJUbBoBLc8gEZaCn4IkPFwLOWVEklKy-oZVJuzqzUr3XVOHOgB64SivYAZUpa5ckkSQtCcBx132CCITAiDHCg4CYKORNVSCYhckEbCil9ZZ-RwRBTXPZEHyBOMnQ9KjqJdL14kkaCe3QVheHlE5K0XPBuBobQ8MNUBCKDEiqDI1ZQHWTZthTB0EyTWMHTTN04UMVpeRcAD+AAZgAVk0nQgA

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:

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:

typescript
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:

typescript
by?: (
  a: NoInfer<Value> | { toString: (() => string) | (() => string); valueOf: (() => string) | (() => Object) },
  z: /* same */,
) => boolean

Since #3727 made these contravariant, an honest comparator over the real value type no longer assigns. Against the real package:

typescript
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:

  1. Drop Expand from EnsureArray:

    typescript
    type EnsureArray<T> = T extends any[] ? T : T[];
  2. Keep Expand, but apply NoInfer outside EnsureArray at the use sites in ComboboxProps, so the conditional still sees a naked type parameter:

    typescript
    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.