Skip to content

Instantly share code, notes, and snippets.

@loucadufault
Last active May 28, 2025 13:30
Show Gist options
  • Select an option

  • Save loucadufault/a2287b8e85c7059ebdb45f910e153876 to your computer and use it in GitHub Desktop.

Select an option

Save loucadufault/a2287b8e85c7059ebdb45f910e153876 to your computer and use it in GitHub Desktop.
TypeScript pattern for enforcing that a tuple have the exact same items as the members of a union (one-liner, avoids linter errors)
import type { IsEqual, TupleToUnion } from 'type-fest'
type IsTupleEqual<TTuple, TExpected> = IsEqual<TupleToUnion<TTuple>, TExpected>
// ---
type ExpectedItem = 'a' | 'b' | 'c'
const myArr = ['b', 'c', 'a'] as const
true satisfies IsTupleEqual<typeof myArr, ExpectedItem> // ts error if myArr has any fewer or excess items