typeof (Type Operator) (TypeScript)

In a type position, the `typeof` operator gives you the type of an existing variable or object. It lets you derive a type from a value instead of writing it out by hand.

TypeScript

JavaScript's `typeof` returns a string at runtime ("string", "number"...). TypeScript reuses the keyword in type positions to mean 'the static type of this value', which is a different, compile-time use.

It pairs well with `keyof` and `as const` to derive types from real data — for example, turning a config object into a type automatically so the type can never drift from the value.

Syntax

type T = typeof value;

Examples

TypeScript

const settings = { theme: "dark", fontSize: 14 };

type Settings = typeof settings;
// { theme: string; fontSize: number }

const other: Settings = { theme: "light", fontSize: 16 };
console.log(other.theme);

Output:

light

Learn it in a lesson

See also

Back to the Dictionary