# The typeof Operator

> Bridging the gap between JavaScript values and TypeScript types.

Subject: TypeScript | Lesson 12 of 54 | 3 min read
Interactive lesson: https://codewithanimation.in/typescript/typeof-operator
Previous: [Intersection Types](https://codewithanimation.in/typescript/intersection-types.md) | Next: [The keyof Operator](https://codewithanimation.in/typescript/keyof-operator.md)

## Two worlds: Values and Types

JavaScript has a `typeof` operator that runs at runtime and returns a string (e.g. `typeof 42 === "number"`).

TypeScript *also* has a `typeof` operator, but it runs entirely during compilation in the 'Type Space'. It takes a JavaScript variable and extracts its exact TypeScript shape.

## Don't Repeat Yourself (DRY)

Often you have a complex configuration object or a default state object. Instead of manually writing an interface that exactly matches that object, you can just let TypeScript infer the type from the object itself using `typeof`.

This ensures your types always stay in perfect sync with your actual data.

## Using typeof in Type Space

When you use `typeof` on the right side of a `type` declaration, you are in Type Space:

```typescript
const defaultTheme = {
  colors: { primary: "blue" },
  spacing: 8
};

// Extracts the exact shape of defaultTheme
type Theme = typeof defaultTheme;

const myTheme: Theme = {
  colors: { primary: "red" },
  spacing: 16
};
```

## Common mistake: The 'Variable used as Type' Error

Beginners often try to use a variable directly as a type constraint:

```typescript
const obj = { id: 1 };

// Error: 'obj' refers to a value, but is being used as a type here.
function process(data: obj) {}
```

You cannot use a value where a type is expected. You must bridge them using `typeof obj`.

## Tip: ReturnType + typeof

You can combine `typeof` with the `ReturnType` utility to extract what a function returns without calling it:

`type Result = ReturnType<typeof fetchUserData>;`

## Try it: Try it

Watch how `typeof` behaves differently depending on whether it's executed in the JavaScript Runtime or the TypeScript Compiler.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/typeof-operator

## Check yourself

### Question 1

What is the difference between JavaScript's typeof and TypeScript's typeof?

- A. They are exactly the same thing.
- B. JS typeof runs at runtime and returns a string; TS typeof runs at compile-time and returns a type shape.
- C. JS typeof only works on primitives; TS typeof only works on objects.

Correct answer: B

TypeScript reuses the 'typeof' keyword, but depending on whether it's used in Value Space (JS) or Type Space (TS), it behaves completely differently.

- Why not A: They are entirely different operators that happen to share a keyword.
- Why not C: Both can work on any value.

## Remember this

- JavaScript `typeof` runs at runtime (Value Space) and returns strings like `"object"`.
- TypeScript `typeof` runs at compile-time (Type Space) and extracts exact type shapes.
- Use `typeof` to derive types from existing configuration objects or default values to stay DRY.
- You cannot use a variable directly as a type; you must use `typeof variable`.
