- Use `let` instead of `var` throughout. All modern browsers support it
and have for many years, even the now-EOL IE11.[^ie]
- Add a `@ts-check` annotation so that anyone working on a TS-enabled
editor will get feedback inline about cases they may otherwise miss.
- Add JSDoc-based type definitions for the script, and fix some errors
that may occur at runtime. (These will be rare if they happen, but
getting rid of them is still a win!)
[^ie]: IE11 did not correctly support the semantics of `let` for `for`
loops, but the uses in the existing code here already used `var` in
a way which would trigger the same behavior as using `let` does.