TLDR Web Dev · ARTICLES & TUTORIALS

THIS CRAZY SYNTAX LETS YOU GET AN ARRAY ELEMENT'S TYPE

totaltypescript.com5 min read

Summary from the TLDR Web Dev newsletter

You can extract the type of an array element in TypeScript using the "Array[number]" syntax, which can be useful for creating enums from arrays and extracting specific types from complex data structures. This technique is called an indexed access type, which lets you access a property on a type by its key. In the case of arrays, using "number" as the key retrieves the type of the array's elements.