Skip to content
webtype.orgwebtype.org

    ↑↓ move · ⏎ open · esc close

    Operators

    keyof

    The union of a type’s keys.

    What it is

    One of the two operators every object transformation is built from — keyof gets the keys out, indexed access reads the values back. The result is a union of literal types in no particular order, and there is deliberately no way to ask for "the first key": order is not something an object type guarantees.

    Examples

    • keyof User
      →keyof User
    • keyof { a: 1 }
      →"a"
    • keyof string[]
      →keyof string[]

      An array has every array method as a key, plus number. This is rarely what you wanted — T[number] is.

    Each resolved type above was printed by TypeScript 6.0.3, not written by hand.

    What it does not do

    • It does not give you an ordered list. Object key order is not part of the type, so nothing can depend on it.
    • It does not skip inherited or built-in members. keyof an array or a class instance includes everything the type declares, methods and all.

    Takeaway

    Reach for keyof T when you want the names and T[keyof T] when you want the values. Almost every mapped type starts with one of those two.