Skip to content

Instantly share code, notes, and snippets.

@mannyanebi
Last active May 12, 2022 18:22
Show Gist options
  • Save mannyanebi/a4ee358610159789ea12d252323a9c6b to your computer and use it in GitHub Desktop.
Save mannyanebi/a4ee358610159789ea12d252323a9c6b to your computer and use it in GitHub Desktop.
How to extract a type from an array
// This is possible with Typescript 2.8+. You can declare an Unpacked<T> type as follows:
type Unpacked<T> = T extends (infer U)[] ? U : T;
type InnerCacheType = Unpacked<CacheType>; // Event | User
// This is a condensed definition of the Unpacked type given in the documentation.
// https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html
type Unpacked<T> = T extends Array<infer U> ? U : T extends ReadonlyArray<infer U> ? U : T;
// this works with ReadonlyArray as well –
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment