Skip to content

Instantly share code, notes, and snippets.

@ryangoree
Last active February 20, 2025 21:19
Show Gist options
  • Select an option

  • Save ryangoree/0e96606da94298b0089650d321171a7e to your computer and use it in GitHub Desktop.

Select an option

Save ryangoree/0e96606da94298b0089650d321171a7e to your computer and use it in GitHub Desktop.
🚧 WIP 🚧 - Get the first member of a tuple that is more or as specific as the member following it.
// 🚧 WIP 🚧 //
/**
* Get the first member of a tuple, {@linkcode T}, that is more or as specific
* as the member following it.
*
* @example
* ```ts
* type Status = "success" | "error" | "idle"
* type ActiveStatus = "success" | "error"
* type CurrentStatus = FirstSpecific<[Status, "idle", ActiveStatus]>
* // -> "idle"
* ```
*/
type FirstSpecific<T extends any[]> = T extends [infer Only]
? Only
: T extends [infer First, ...infer Rest]
? First | Rest[number] extends First
? FirstSpecific<Rest>
: First
: T[number]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment