Check polymorphic props and ref types
Reuse native props instead of recreating them
ComponentPropsWithoutRef extracts the props of a component or intrinsic element without its ref; ComponentPropsWithRef includes the ref contract when supported. This lets a wrapper accept native accessibility and event attributes without manually listing them. Omit keys that the wrapper owns, and keep the final native type visible to the caller.
import React,{useState} from 'react';import type {ComponentPropsWithoutRef} from 'react';type Props=Omit<ComponentPropsWithoutRef<'button'>,'type'>&{tone?:'plain'|'strong'};function Action({tone='plain',...props}:Props){return <button {...props} type="button" data-tone={tone}/>;}export default function App(){const [count,setCount]=useState(0);return <><Action tone="strong" onClick={()=>setCount(c=>c+1)}>Pack item</Action><p>{count}</p></>;}Click Pack item to increment the count. The wrapper fixes type to button, which prevents accidental form submission, while preserving native disabled, onClick and aria attributes. It does not expose a submit variant; if needed, define a separate explicit contract. The placement of type after the spread also protects the fixed attribute at runtime.
Choose a bounded polymorphic contract
import React from 'react';import type {ComponentPropsWithoutRef} from 'react';type LinkProps={as:'a'}&Omit<ComponentPropsWithoutRef<'a'>,'href'>&{href:string};type ButtonProps={as:'button';href?:never}&Omit<ComponentPropsWithoutRef<'button'>,'type'>;function Control(props:LinkProps|ButtonProps){if(props.as==='a'){const {as,...rest}=props;return <a {...rest}/>;}const {as,href,...rest}=props;return <button {...rest} type="button"/>;}export default function App(){return <><Control as="a" href="#details">Details</Control><Control as="button" onClick={()=>{document.getElementById('details')?.focus();}}>Focus details</Control><h2 id="details" tabIndex={-1}>Details</h2></>;}The link navigates to a fragment; the button moves focus to the heading. The as discriminator ties the allowed attributes and event types to the rendered element. A button with href or an anchor missing href should fail type checking. A fully generic as API can use ElementType and ComponentPropsWithRef, but inference, ref variance and collisions make it harder to maintain; use a bounded union or separate components when the supported set is small. This example does not validate untrusted URLs; the href is a fixed local fragment.
Expose a ref deliberately and document versions
import React,{useRef} from 'react';import type {ComponentPropsWithRef} from 'react';type Props=ComponentPropsWithRef<'input'>&{label:string};function Field({label,ref,...props}:Props){return <label>{label} <input {...props} ref={ref}/></label>;}export default function App(){const ref=useRef<HTMLInputElement>(null);return <><Field label="Supply name" ref={ref}/><button onClick={()=>ref.current?.focus()}>Focus supply name</button></>;}Clicking the button should focus the input. React 19 accepts ref as a prop on function components; with React 18 use forwardRef<HTMLInputElement, PropsWithoutRef> and pass its second argument to the native input. Keep matching type packages for each lane. The wrapper promises an input node, not an arbitrary HTMLElement. An imperative ref is an escape hatch; prefer declarative props for ordinary state. To test contracts, use strict tsc with noEmit and expected-error cases; a successful syntax transform does not verify React declarations or focus behavior.
Practice
Reject href on the button branch and missing href on the anchor branch in a type test. Pass a textarea ref to Field and expect a type error with matching React declarations. Restore valid code, then check keyboard activation and focus in the browser.