TypeScriptTips

Make bugs impossible.
One TypeScript tip at a time.

I like the tricks :) – Kasia

Type-safe carousel

Instead of keeping the index and array separated, use a zip list.

type SlideDeck = {
  slides: Slide[],
  selected: number,
}

const carousel: SlideDeck = {
  slides: [], // ⛔️ There should be at least one
  selected: -10, // ⛔️ WTH?
}
type SlideDeck = {
  before: Slide[],
  current: Slide,
  after: Slide[],
} // ✅ Always one slide with 0 or more before and after