TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Narrow types

Avoid casting, especially to any.

const element = document.querySelector('#id')

(element as any).style.display = 'block'
// ⛔️ element could be null
const element = document.querySelector('#id')

if (element instanceof HTMLElement) {
  element.style.display = 'block'
}