Linting TypeScript With typescript-eslint

Door-check passes npm run check with every package typed, but one mistake still gets through. You’ve met it before. A promise that no code uses doesn’t trigger a checker error, and only a linter rule catches it.

In this lesson, you’ll add typescript-eslint to door-check so that rule can read your types. You’ll give every file the config it needs and make npm run check run the checker and the linter together. Start with the save that slips through.

Turn on the Type-Aware Rules

Door-check’s click handler needs to send each check-in to Mei Chen’s box office, so she can count who’s in the house before the curtain goes up. Tobias Okonkwo got the save from an AI assistant. Add it to src/check-in.ts, below the partySize line, together with a boxOfficeUrl line that reads the box office’s address the way src/main.ts does:

Language: TypeScript Filename: src/check-in.ts
import { doorList } from "./door-list.ts";

const list = document.querySelector("#door-list");
const partySize = document.getElementById("party-size");
const boxOfficeUrl = import.meta.env.VITE_BOX_OFFICE_URL;

async function saveCheckIn(
  reference: string,
  arrived: number,
): Promise<void> {
  const response = await fetch(`${boxOfficeUrl}/check-ins`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ reference, arrived }),
  });
  if (!response.ok) {
    throw new Error(`Box office answered ${response.status}`);
  }
}

Then add the highlighted call as the last line of the click handler, so the save starts after the button shows who’s in:

Language: TypeScript Filename: src/check-in.ts
  booking.checkedIn = true;
  button.disabled = true;
  button.textContent = `In: ${arrived} of ${booking.partySize}`;
  saveCheckIn(booking.reference, arrived);
});

Here’s what the two tools say about that call. The checker’s verdict reflects the current setup, and ESLint’s is what you’ll have once you’ve set it up in this lesson:

Language: Shell
$ npx tsc --noEmit -p tsconfig.json
$ npx eslint src/check-in.ts

/.../door-check/src/check-in.ts
  47:3  error  Promises must be awaited, end with a call to .catch, end with a
call to .then with a rejection handler or be explicitly marked as ignored with
the `void` operator  @typescript-eslint/no-floating-promises

✖ 1 problem (1 error, 0 warnings)

The checker prints nothing, because a promise that isn’t awaited is still a valid value. ESLint reports line 47, the saveCheckIn() call, with the rule’s name, no-floating-promises, last.

A floating promise is one that no code awaits or handles. If this one rejects, the browser reports it in the Console, but your code never reacts, and the button still says the party is in. The rule can spot it only if it knows that saveCheckIn() returns a promise, so it needs the types.

You set up ESLint in Pull Requests, Linting and Formatting, and typescript-eslint gives it those types. Its parser lets ESLint read TypeScript syntax, and its type-aware rules ask the checker for each value’s type. Install ESLint, its own recommended rules, and typescript-eslint as development dependencies:

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

Become a Member to join the conversation.