Skip to content
webtype.orgwebtype.org

    ↑↓ move · ⏎ open · esc close

    Operators

    typeof

    The bridge from a value to its type.

    What it is

    Types and values live in separate namespaces, and typeof is the only way across in one direction. In a type position it means "the type of that value", which is unrelated to the JavaScript typeof operator that returns a string at run time — same spelling, different worlds, and one of the few genuine puns in the language.

    Examples

    • typeof config
      →{ retries: number; url: string; }
    • typeof send
      →(to: string) => void
    • keyof typeof config
      →"retries" | "url"

      The pairing you will write most often: cross into the type world, then take the keys.

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

    What it does not do

    • It does not accept an expression. typeof foo.bar works because that is a reference; typeof foo() does not, because calling something is not a value you can name.
    • It does not narrow. typeof in a type position reads the declared type, not whatever the variable has been narrowed to at that line.

    Takeaway

    When a type is already described perfectly by a value you have, do not write it twice. typeof keeps the two from drifting apart.