What is the typeof Operator in TypeScript?
Learn how TypeScript's type-level typeof operator derives types from existing values, pairs with keyof and as const, and keeps your types in sync.
Expected Interview Answer
In TypeScript the typeof operator, when used in a type position, captures the static type of an existing variable or object so you can reuse it as a type without redeclaring the shape.
JavaScript already has a runtime typeof that returns strings like 'string' or 'object'. TypeScript adds a second, type-level meaning: writing typeof someValue in a type annotation produces the inferred type of that value. It is commonly combined with const objects, keyof, and generics to derive types from a single source of truth instead of maintaining a separate interface.
- Derives types from real values, keeping them in sync
- Avoids duplicating a shape as both a value and an interface
- Pairs with keyof to extract union types of keys
- Works great with 'as const' for literal-typed config objects
- Reduces drift between runtime data and static types
AI Mentor Explanation
Think of a team sheet the captain has already filled in with every player, role, and batting order. Instead of rewriting that sheet for the scorers, you just point at it and say 'use exactly this'. The typeof type operator does the same: it points at an existing value and reuses its full shape as a type, so you never re-author the line-up by hand.
Step-by-Step Explanation
Step 1
Create a value
Define a concrete object or variable, often with 'as const' to preserve literal types.
Step 2
Apply typeof in a type position
Write 'type T = typeof myValue' to capture that value's inferred static type.
Step 3
Combine with keyof
Use 'keyof typeof myValue' to get a union of the object's keys as a type.
Step 4
Reuse the derived type
Annotate functions, parameters, or state with the derived type instead of hand-writing an interface.
Step 5
Let inference update it
When the source value changes, the derived type updates automatically, avoiding drift.
What Interviewer Expects
- Awareness that typeof has both a runtime and a type-level meaning
- Ability to use typeof in a type position to capture a value's type
- Knowledge of pairing typeof with keyof and 'as const'
- Understanding it creates a single source of truth for types
- A concrete example deriving a type from a config object
Common Mistakes
- Confusing the runtime typeof (returns a string) with the type-level typeof
- Trying to use typeof on a type name instead of a value
- Forgetting 'as const', so literals widen to string/number
- Assuming typeof works on values not yet declared in scope
- Overusing it where a plain interface would be clearer
Best Answer (HR Friendly)
“TypeScript's typeof lets you take an existing variable or object and reuse its exact shape as a type, instead of describing that shape twice. It keeps your code and its types in sync because the type comes straight from the real value.”
Code Example
const settings = {
theme: 'dark',
retries: 3,
features: ['search', 'export'],
} as const;
// Capture the value's static type
type Settings = typeof settings;
// Union of keys: 'theme' | 'retries' | 'features'
type SettingKey = keyof typeof settings;
function readSetting(key: SettingKey): Settings[SettingKey] {
return settings[key];
}Follow-up Questions
- How does the type-level typeof differ from JavaScript's runtime typeof?
- What does 'as const' change about the inferred type?
- How do you combine typeof with keyof to build a union of keys?
- Can you use typeof on the return value of a function?
- When would you prefer a hand-written interface over typeof?
MCQ Practice
1. In a type position, what does 'typeof myObj' produce?
Used in a type position, typeof yields the inferred static type of the referenced value.
2. Why is 'as const' often paired with typeof?
'as const' preserves literal types so typeof captures exact values rather than widened primitives.
3. What does 'keyof typeof obj' return?
typeof captures the type, and keyof extracts a union of that type's property keys.
Flash Cards
What are the two meanings of typeof in TypeScript? — A runtime operator returning a string, and a type-level operator capturing a value's static type.
How do you get a value's type? — Write 'type T = typeof value' in a type position.
What does 'keyof typeof obj' give you? — A union type of the object's property keys.
Why use 'as const' with typeof? — To keep literal types precise instead of widening to string/number.