Starting with TypeScript 2.8, you can also do this with the infer :
type GetElementType<T extends Array<any>> = T extends (infer U)[] ? U : never;
For instance:
// ElementType === string type ElementType = GetElementType<string[]>;
And this also works inside mapped types, where different properties can have different types:
type MapArraysToValues<T extends { [key: string]: any[] }> = { [key in keyof T]: GetElementType<T[key]>; }; // Output === { x: number, y: string } type Output = MapArraysToValues<{ x: number[], y: string[] }>;
source share