Last active
February 20, 2025 21:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // π§ 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