typeof and keyof

typeof in a type position takes a type out of a value. It is not the JavaScript operator; same word, different world.

That is genuinely useful for configuration objects and constant maps, where the value is the source of truth and writing the type by hand would just be a second thing to maintain.

Try it

Add a field to config and it becomes available to get immediately, with the right type and with no type written by hand.

loading editor…

checking…

get returns exactly the right type per key: a number for port, a string for host. The last line is rejected for that reason, which is not expressible without keyof.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back