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 anhrefonto a<span>.typecannot be required underas="button". A<button>defaults tosubmitand this component sets no default of its own, so a card-button inside a form submits it — the defect ADR-0033 found onAdminUsersPage. The type could only ask nicely, in a comment.- The ref has no element to name. 002's row-39 sweep declared it at
HTMLElementand said so out loud: a<div>claim is false at exactly the call sitesasexists 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:
- 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 oncedisabledexisted on the button arm. The arms that are not controls therefore statehref/target/rel/type/disabledasnever. Discrimination closes the case whereasis present; theneverguards close the case where it is absent. - The declaration stops being a
ForwardRefExoticComponent. That wrapper takes one props type and this component now has four, andPropsWithoutRefover a union would strip the veryrefeach arm exists to type. The component is declared as a call signature returningReactElement | null; nothing in either tree reads the runtime'sdisplayNameorpropTypes.
Consequences
- No runtime change and no served byte moves:
surfaces.jsxalready spread whatever it was given onto whateverasnamed. 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 atypeis 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
HTMLElementref bound survives only on the fourth arm, whereasis genuinely unknown and there is nothing narrower to know. - The rule generalises and is not yet applied everywhere.
Statcarries the same flathref/tobesideas, recorded againstCardPropsin its own docstring;StackandFilterRoware 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