Last active
April 23, 2024 10:36
-
-
Save ReVoid/7dca5529d247f513b191ac286ef05de8 to your computer and use it in GitHub Desktop.
Extract array item with conditional types
This file contains 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
// with infer keyword | |
type ArrayItemInfer<T> = T extends Array<infer I> ? I : never; | |
// or just with regular index type 🙃 | |
type ArrayItemRegular<T extends Array<any>> = T[number]; | |
type People = { firstName: string; lastName: string; }[]; | |
type PersonInfer = ArrayItemInfer<People>; // { firstName: string; lastName: string; } ✅ | |
type PersonRegular = ArrayItemRegular<People>; // { firstName: string; lastName: string; } ✅ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment