Skip to content
Toolkit

ADR-0152 — A polymorphic component's props are discriminated on the element

Status: Accepted · Date: 2026-08-31 · Track: 002 close-out (LEDGER drain, row 44) · Supersedes: nothing · Builds on: ADR-0033, ADR-0034

Context

Card renders whatever as says. The layer ships element rules for exactly two of those elements — a.lk-card and button.lk-card — so the prop is not decoration: it is how a card becomes a control. Its type declared the attributes those two elements need flat, beside as?: ElementType:

export interface CardProps extends HTMLAttributes<HTMLElement> {
  as?: ElementType;
  href?: string; target?: string; rel?: string;
  type?: 'button' | 'submit' | 'reset'; disabled?: boolean;
}

Three things follow from that shape, and the docstring admitted all three in prose rather than in the type:

  • <Card as="span" href="/x"> type-checks, and the runtime spreads an href onto a <span>.
  • type cannot be required under as="button". A <button> defaults to submit and this component sets no default of its own, so a card-button inside a form submits it — the defect ADR-0033 found on AdminUsersPage. The type could only ask nicely, in a comment.
  • The ref has no element to name. 002's row-39 sweep declared it at HTMLElement and said so out loud: a <div> claim is false at exactly the call sites as exists for, and narrowing it properly means discriminating the props first.

Decision

The props of a component that chooses its own element are a union discriminated on as, and each arm carries the attributes that element takes and the node its ref receives. CardProps becomes four arms — the default div, a, button, and every other element — over one CardOwnProps that holds what a card is in any of them (hover, flush, size, selected). The button arm requires type.

Two consequences worth stating, because both were surprises:

  1. A union alone does not refuse a foreign attribute. TypeScript permits a property any one constituent declares, so <Card disabled> on the default arm still type-checked once disabled existed on the button arm. The arms that are not controls therefore state href/target/rel/type/disabled as never. Discrimination closes the case where as is present; the never guards close the case where it is absent.
  2. The declaration stops being a ForwardRefExoticComponent. That wrapper takes one props type and this component now has four, and PropsWithoutRef over a union would strip the very ref each arm exists to type. The component is declared as a call signature returning ReactElement | null; nothing in either tree reads the runtime's displayName or propTypes.

Consequences

  • No runtime change and no served byte moves: surfaces.jsx already spread whatever it was given onto whatever as named. This is the type learning what the component always did — the shape row 38/39 established for refs, one axis over.
  • <Card as="button"> without a type is now a compile error rather than a hazard documented in a docstring. The app's one such call site (CataloguePage) already passed it.
  • The wide HTMLElement ref bound survives only on the fourth arm, where as is genuinely unknown and there is nothing narrower to know.
  • The rule generalises and is not yet applied everywhere. Stat carries the same flat href/to beside as, recorded against CardProps in its own docstring; Stack and FilterRow are polymorphic with no element-specific attributes at all, so they have nothing to discriminate and stay as they are. A component that gains one is discriminated when it does, not before.
  • The cost is a longer type and worse error text: a mismatch reports against the arms rather than naming the prop. That is the trade a union makes, and the four arms are named in the docstring so the reader knows which one they meant.

Source: docs/decisions/0152-a-polymorphic-components-props-are-discriminated-on-the-element.md

Library