Search code examples
typescripttypeof

What's the meaning of typeof Array[number] in Typescript?


const names = ['jacob', 'master jung', 'kyuhyun'] as const;
type Names = typeof names[number];

enter image description here

I got the results I wanted, but I don't understand typeof names[number].

What does typeof Array[number] in Typescript mean?


Solution

  • typeof gets the type of names variable (which is readonly ['jacob', 'master jung', 'kyuhyun']) then array/tuple member type is resolved. This is called indexed access types or lookup types.

    Syntactically, they look exactly like an element access, but are written as types

    In this case we "query" the type of tuple member (tuple/array at index) which is 'jacob' | 'master jung' | 'kyuhyun'

    Playground