Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Selecting Elements With the Right Type

In this lesson, you selected door-check’s search box so the checker knows it’s an input, and you put the LST- prefix in it with an instanceof check that runs as a real test in the browser.

Key Points

  • The checker reads src/main.ts before anything runs and never opens index.html. From a class selector such as ".ref-search", all it has is a string, so document.querySelector() gives Element | null.
  • Element is the DOM library’s type for any element at all, a heading, a list or an input. It only promises what every element has, so it has no value. The first draft reports two errors on one line: TS18047, 'search' is possibly 'null', and TS2339, Property 'value' does not exist on type 'Element'.
  • A tag name tells the checker more: document.querySelector("input") gives HTMLInputElement | null. But a page often has more than one input, so keep the class and check the element instead.
  • Besides the type, the browser has a real class named HTMLInputElement, and every input is an instance of it. search instanceof HTMLInputElement is false for null, so one test covers both errors.
  • The checker follows it branch by branch: Element | null above the if, HTMLInputElement inside it, and Element | null again in the else branch, because a false test means nothing matched or the element isn’t an input. Element isn’t a union with an input member, so there’s nothing to take out.
  • The types at each line were the checker’s own, worked out from the code. None of these labels is written in the file. So Vite has no type syntax to remove, and the instanceof test runs in the browser as written. If the search box goes missing, the else branch logs No booking-reference input: null.
  • A type argument, document.querySelector<HTMLInputElement>(".ref-search"), claims the element is an input, but the result is still HTMLInputElement | null, so you still need a null check. The claim is removed before the code runs, so nothing checks it.

Final Code

The page keeps its search box as it was, and only the end of src/main.ts changes. The selection stays on the class, and the instanceof check does the rest: it rules out null and any element that isn’t an input, for the checker and in the browser alike. The search box in index.html:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’re back in door-check, the page Rosa Delgado’s ushers use at the door. In this video, you’ll select an element from the page so the checker knows what it is.

00:10 Every booking reference at the theater starts with the same four characters. To save the ushers some typing, the page should put them in the search box when it loads.

00:20 The search box is an input in index.html, and ref-search is its class. At the end of main.ts, here’s a first draft. It selects the element by its class, then sets its value to the prefix.

00:34 Vite runs the code without checking its types, so the draft works on tonight’s page. Run npm run check, though, and the checker reports two errors on the second line.

00:46 Read them from the top. The first says search is possibly null. querySelector gives you null when nothing on the page matches, and the checker wants that case handled.

00:58 The second says property value doesn’t exist on type Element.

01:03 Element is the DOM library’s type for any element at all. A heading is an Element, and so is a list or an input. Element only promises what every element has, and value isn’t one of them.

01:18 But the page does have an input with that class. Why doesn’t the checker know?

01:24 The checker reads main.ts before anything runs, and it never opens index.html. All it has is the selector, a string. A class could be on any element, or on none, so the checker types search as Element or null.

01:41 A tag name tells it more. Select by the tag name instead, the word input, and the type becomes HTMLInputElement or null, because the DOM library knows that tag.

01:54 But a page often has more than one input, and the first isn’t always the search box. Keep the class, and check the element instead.

02:04 Wrap the assignment in an if statement with an instanceof test, so it runs only for a real input. The browser also has a real class named HTMLInputElement, and every input is an instance of it.

02:17 That test is false for null, so it covers both errors. The checker follows the test branch by branch. Above the if, search is Element or null. Inside the if, it’s HTMLInputElement, so setting its value passes.

02:34 The else branch logs a message with search when the test fails. What type does search have on that line? Your turn: pause the video and predict the type.

02:49 It’s Element or null, the same type as above the if. A false test means nothing matched, or the element isn’t an input. Element isn’t a union with an input part, so the checker has nothing to take out.

03:03 Run npm run check again, and it passes.

03:08 Does the instanceof test still run in the browser? None of these labels is written in the file. They were the checker’s own. So Vite has nothing to remove.

03:18 The test is plain JavaScript, and it runs as it is. Open the page, and the search box shows the prefix. If the search box goes missing, the else branch runs, and the Console logs the message with null.

03:32 You may also meet a type argument after querySelector, in angle brackets. It claims the element is an input, but the result can still be null. The claim is removed before the code runs, so nothing checks it.

03:47 So the checker can’t read your HTML. From a class, it knows only Element or null. An instanceof check narrows it for the checker, and it runs as a real test in the browser.

03:59 The search box now has its type. Next, in Typing Event Handlers, you’ll make it respond as the ushers type, and see what type an event’s target gets.

Become a Member to join the conversation.