Skip to content

Instantly share code, notes, and snippets.

@ReVoid
Last active April 23, 2024 10:36
Show Gist options
  • Save ReVoid/7dca5529d247f513b191ac286ef05de8 to your computer and use it in GitHub Desktop.
Save ReVoid/7dca5529d247f513b191ac286ef05de8 to your computer and use it in GitHub Desktop.
Extract array item with conditional types
// 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