Created
June 28, 2024 05:46
-
-
Save Phryxia/049a75f56d5688525b0426c9efcec1dd to your computer and use it in GitHub Desktop.
TypeScript snippet for extracting key type of array and tuple
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 KeyOfArrayLike<T> = | |
T extends [] ? | |
never | |
: T extends [unknown, ...infer R] ? | |
R['length'] | KeyOfArrayLike<R> | |
: T extends unknown[] ? | |
number | |
: never |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This type is useful when you don't know actual
T
during inference of complex generic type.