ARIA Reflection Properties On Elements

ARIA states expose typed reflection properties such as ariaInvalid and ariaLive, replacing setAttribute strings with compiler-checked assignments.

TypescriptForm Validation

Code

typescriptsnippet
32 lines
// ARIA states have typed reflection properties on Element: no setAttribute
// strings, and unknown attribute names fail to compile.
interface FieldRefs {
  input: HTMLInputElement;
  error: HTMLElement;
}

function bindField(inputId: string, errorId: string): FieldRefs | null {
  const input = document.getElementById(inputId);
  const error = document.getElementById(errorId);
  if (!(input instanceof HTMLInputElement) || !error) return null;
  return { input, error };
}

function reportProblem(field: FieldRefs, message: string): void {
  field.input.ariaInvalid = 'true';
  field.error.ariaLive = 'assertive';
  field.error.textContent = message;
}

function clearProblem(field: FieldRefs): void {
  field.input.ariaInvalid = 'false';
  field.error.textContent = '';
}

const field = bindField('email', 'email-error');
if (field) {
  reportProblem(field, 'Email is missing an @');
  console.log('aria-invalid:', field.input.ariaInvalid);
  clearProblem(field);
  console.log('aria-invalid:', field.input.ariaInvalid);
}

How it works

Everything happens inside the functions bindField, reportProblem and clearProblem.

The result comes back as an object carrying input.

What to watch for

  • Only the runtime is needed — no packages to fetch and no installs.
  • document exist only in browsers — a Node runtime has no shims for them out of the box.

Using it

The signature doing the main work is function bindField(inputId: string, errorId: string)

The run emits 1 labeled section — aria-invalid: — one per step.

The call returns input.

The key statements, annotated

if (!(input instanceof HTMLInputElement) || !error) return null;
return { input, error };

Interactive Tools for This Topic

FAQ

Does `bindField` modify its input?
No. Nothing passed in is written to, which makes the call safe on shared data.
Does ARIA Reflection Properties On Elements run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.