Last active
September 6, 2020 09:07
-
-
Save roalcantara/aaf7f436e042db24963f7cfcf4cfbe15 to your computer and use it in GitHub Desktop.
TypeScript 3.9 | Advanced Types: Extracts a tuple's last entry
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
type Head<T extends any[]> = T extends [any, ...any[]] ? T[0] : never | |
type HasTail<T extends any[]> = T extends [] | [any] ? false : true | |
type Tail<T extends any[]> = ((...t: T) => any) extends ( | |
_: any, | |
...tail: infer TT | |
) => any | |
? TT | |
: [] | |
type Last<T extends any[]> = { | |
0: Last<Tail<T>> | |
1: Head<T> | |
}[HasTail<T> extends true ? 0 : 1] | |
type LastEntry = Last<[1, 2, 3, 4]> // 4 | |
// https://medium.com/free-code-camp/typescript-curry-ramda-types-f747e99744ab | |
// https://github.com/millsp/medium/blob/master/types-curry-ramda/src/index.ts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment