Sitemap

Member-only story

Mastering `typeof` and `keyof` in TypeScript: Every Real-World Use You Need to Know

5 min readJun 17, 2025

In this article, I’ll break down what `typeof` and `keyof` operators do, why they’re indispensable, and how you can use them like a pro. No fluff. Just the straight talk you need.

❤️ Not a member? Click here to read this article for free.

What are typeof and keyofs are ?

  • typeof is used to get the type of a variable or expression. It allows you to capture the type information of a value to reuse it elsewhere in your code.
  • keyof is a type operator that extracts the keys of an object type as a union of string literal types. It’s useful for creating types based on the property names of an object.

Code Examples:

1. Extract type from a variable with typeof

const settings = { darkMode: true, version: 2 };
// Extract the type of 'settings' automatically


type SettingsType = typeof settings;

// SettingsType is:
// {
// darkMode: boolean;
// version: number;
// }

2. Get keys of a type with keyof

const settings = { darkMode: true, version: 2 };

type SettingsKeys = keyof SettingsType;

// "darkMode" | "version"

--

--

Kristiyan Velkov
Kristiyan Velkov

Written by Kristiyan Velkov

Front-end Advocate | Meta Certified React JS Developer | Tech lead | Speaker | Book Author| React.js | Next.js | Angular | TS | DevOps | People management

No responses yet