π§  TypeScript β typeof Type Operator: Explained with Examples
π§² Introduction β Understanding typeof in TypeScript
In TypeScript, the typeof operator is more than just a runtime tool β it can also be used in type positions to infer or reference the type of a value. This enables strong typing based on existing variables or constants, ensuring type safety without code duplication.
π― In this guide, youβll learn:
- What the typeofoperator does in a type context
- Difference between JavaScript typeofand TypeScripttypeof
- Use cases with variables, functions, arrays, and type aliases
- How typeofworks withkeyofand indexed types
π What Is typeof in TypeScript?
In JavaScript, typeof is used at runtime to check the type of a value.
console.log(typeof "hello"); // "string"
In TypeScript, typeof is also used at compile time to get the type of a variable, constant, or function β not just its runtime type.
let user = { name: "Alice", age: 30 };
type User = typeof user;
// Equivalent to: { name: string; age: number }
π§± Basic Syntax of typeof Type Operator
type NewType = typeof someVariable;
This copies the type definition from the variable into the type NewType.
π Examples: typeof in Action
π§ͺ Example 1: Typing Based on Variable
const point = { x: 10, y: 20 };
type Point = typeof point;
// Point is now: { x: number; y: number }
π§ͺ Example 2: Use with Functions
function greet(name: string) {
  return `Hello, ${name}`;
}
type GreetFn = typeof greet;
// GreetFn is (name: string) => string
π§ͺ Example 3: typeof with Arrays
const roles = ["admin", "user", "guest"] as const;
type Role = typeof roles[number];
// Role = "admin" | "user" | "guest"
π typeof + keyof: Dynamic Type Utilities
const settings = {
  theme: "dark",
  notifications: true,
};
type Settings = typeof settings;
type SettingsKeys = keyof typeof settings;
// "theme" | "notifications"
This combo allows powerful manipulation of types at scale.
π οΈ Practical Use Cases
| Use Case | How typeofHelps | 
|---|---|
| π Create type from an existing object | Avoids duplication | 
| βοΈ Type safe function references | Extract function type signatures | 
| π Use dynamic keyofand indexed access | Works with mapped/utility types | 
| π§° Works with ReturnType,Parameters, etc. | Improves reusability and inference | 
π« Common Mistakes
β Using typeof on a type instead of a value
type Person = { name: string };
// β Error: Cannot use 'typeof' with a type
type T = typeof Person;
β Instead:
const person = { name: "Alice" };
type T = typeof person;
π typeof with Modules or Imported Data
import config from "./config.json";
type ConfigType = typeof config;
This ensures your type stays synchronized with dynamic data.
π Summary β Recap & Next Steps
The typeof operator in TypeScript allows you to extract types from values and functions, promoting DRY (Don’t Repeat Yourself) principles and maintainability.
π Key Takeaways:
- Use typeofto derive a type from a runtime value
- Combine with keyof,in,ReturnType,Parametersfor advanced utilities
- Enables safer, scalable typing for functions, objects, arrays, and constants
βοΈ Real-world relevance:
Perfect for large projects needing robust type consistency, especially in libraries and shared modules.
β FAQs
β What’s the difference between JS and TS typeof?
β
 JavaScript’s typeof runs at runtime and returns strings. TypeScript’s typeof in type positions refers to the type of a value at compile time.
β Can I use typeof on a function?
β Yes, you can extract the function signature:
function add(a: number, b: number): number {
  return a + b;
}
type AddType = typeof add; // (a: number, b: number) => number
β How do I combine typeof with literal types?
Use as const:
const roles = ["admin", "editor"] as const;
type Role = typeof roles[number]; // "admin" | "editor"
Share Now :
