← Back to TypeScript Course | Chapter 12: TypeScript with DOM | Lesson 5 of 6

Working with Forms

TypeScript provides DOM types such as HTMLFormElement and HTMLInputElement for working with forms. These types make submission, form values, FormData, and validation easier to handle safely.
Syntax
typescript
const form = document.querySelector("form") as HTMLFormElement;
form.addEventListener("submit", (e: SubmitEvent) => {
  e.preventDefault();
});

Creating a Typed Form

Typing a form element as HTMLFormElement gives you access to its .elements collection and its submit/reset methods with proper autocomplete, instead of treating it as a generic, undifferentiated element.

Example: Creating a Typed Form

typescript
const form = document.createElement("form") as HTMLFormElement;
console.log(form.elements.length, typeof form.submit);

Handling Form Submission

Handling form submission means listening for the submit event and calling event.preventDefault() inside a handler typed to receive a SubmitEvent, which stops the browser's default full-page reload.

Example: Handling Form Submission

typescript
const form = document.createElement("form");
form.addEventListener("submit", (event: SubmitEvent) => {
  event.preventDefault();
  console.log("Submission prevented");
});
form.dispatchEvent(new SubmitEvent("submit", { cancelable: true }));

Reading Input Values

Reading input values safely means narrowing event.target to HTMLInputElement first, since the base event type has no .value property — skipping this narrowing is a very common source of TypeScript form-handling errors.

Example: Reading Input Values

typescript
const input = document.createElement("input");
input.value = "hello";
input.addEventListener("input", (event: Event) => {
  const target = event.target as HTMLInputElement;
  console.log(target.value);
});
input.dispatchEvent(new Event("input"));

FormData

The FormData API lets you collect every named field's value from a submitted form at once, and typing it correctly means knowing its .get() method returns FormDataEntryValue | null, not a bare string.

Example: FormData

typescript
const form = document.createElement("form");
form.innerHTML = '<input name="username" value="ravi">';
const data = new FormData(form);
const value: FormDataEntryValue | null = data.get("username");
console.log(value);

Form Validation

Form validation in TypeScript usually combines HTML's built-in constraint attributes (like required) with typed custom logic that runs on submit, since the compiler alone can't guarantee a user actually filled a field in correctly.

Example: Form Validation

typescript
const input = document.createElement("input");
input.required = true;
input.value = "";
function isValid(el: HTMLInputElement): boolean {
  return !el.required || el.value.trim().length > 0;
}
console.log(isValid(input));
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting event.preventDefault() in the submit handler, so the page reloads.
  2. Reading form.elements["name"].value without a cast, since the element is not known to be an input.
  3. Assuming FormData.get("age") returns a number, when it returns FormDataEntryValue | null (a string or file).
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.