lib DOM types & querySelector (strict null checks)
Understand lib DOM types and select elements safely with querySelector using strict null checks.
lib DOM types & querySelector (strict null checks) is a free TypeScript Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the TypeScript Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Intro
Goal: Use lib DOM types and querySelector safely. You will check for null first, then use narrowed types for element APIs.
DOM lib types
The TS DOM lib provides types for browser globals (e.g., Document, HTMLElement, Event), powering IntelliSense and checks.
// DOM types come from the lib DOM definitions
// Examples: Document, HTMLElement, HTMLInputElement, Event
const title: string = document.title; // Document API
console.log("Title:", title);querySelector type
querySelector returns Element | null. With strictNullChecks, you must handle the null case explicitly.
const el = document.querySelector("#app");
// el: Element | null with strictNullChecks
console.log("Found?", el !== null);Early return guard
Guard early: check for null and return or throw. After the guard, the variable is non-null in that branch.
function mount() {
const root = document.querySelector("#app");
if (!root) {
console.warn("No #app found");
return; // root is narrowed below only if present
}
// root: Element
root.innerHTML = "<strong>Hello TS + DOM</strong>";
}
mount();Subtype narrowing
Narrow further with instanceof to a specific subtype like HTMLInputElement so element-specific APIs are available.
function readInputValue() {
const input = document.querySelector("#name");
if (!(input instanceof HTMLInputElement)) {
return; // not an <input>
}
// input: HTMLInputElement
console.log("value=", input.value);
}
readInputValue();Non-null assertion caution
Avoid the non‑null assertion (!) unless you are absolutely sure. Guards keep runtime safer.
function bad() {
const root = document.querySelector("#app")!; // unsafe if #app missing
// Potential runtime error below if root is actually null
root.textContent = "unsafe";
}
// Prefer guarded version instead of non-null assertionGuard pattern check
Quick check: After const el = document.querySelector("#app") with strict null checks, what's the safest next step?
Recap
Recap: DOM types power IntelliSense. querySelector returns Element | null; guard first, then use. Narrow with instanceof for element‑specific APIs.
Frequently asked questions
Is the “lib DOM types & querySelector (strict null checks)” lesson free?
Yes — the full text of “lib DOM types & querySelector (strict null checks)” is free to read here on the web, and the TypeScript Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the TypeScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “lib DOM types & querySelector (strict null checks)”?
Understand lib DOM types and select elements safely with querySelector using strict null checks. You practise TypeScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start TypeScript Academy?
No prior experience is required. TypeScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “lib DOM types & querySelector (strict null checks)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this TypeScript Academy lesson?
Yes. Every TypeScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- lib DOM types & querySelector (strict null checks)
- Event types, custom events; narrowing HTMLElement subtypes
- Working with FormData, URL, URLSearchParams