DEV Community

Discussion on: TypeScript - get types from data using typeof

Collapse
 
andyviv profile image
Andy Perlitch

Thanks for the post. Is there documentation on that typeof myConst[number] syntax? Specifically the [number] part is something i've never seen in typescript, and can't find on the doc site.

Collapse
 
andreasbergqvist profile image
Andreas Bergqvist

Hi, I'm not sure. Been a while since I wrote this and its a gathering of things I read and things I just tested with code...

Collapse
 
karataev profile image
Eugene Karataev • Edited

New TS Handbook has a mention about [number] syntax:

const MyArray = [
    { name: "Alice", age: 15 },
    { name: "Bob", age: 23 },
    { name: "Eve", age: 38 }
];
type T = (typeof MyArray)[number];
Enter fullscreen mode Exit fullscreen mode