How do I extract a type from an array in typescript?

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;

Now you can do

type InnerCacheType = Unpacked<CacheType>; // Event | User

This is a condensed definition of the Unpacked type given in the documentation.


This could also be achieved with a type query. ie.

type CacheType = Event[] | User[];
type InnerCacheType = CacheType[number]; // Event | User

TS Playground

If you look at this comment from the ts dev team

You're allowed to write

const t: Array<string>[number] = "hello";

as a roundabout way of writing const t: string

So This will always hold true.