TypeScript typeof Operator

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


In TypeScript, the typeof operator can be used in two ways:

  • At runtime, it returns the type of a value as a string (string, number, etc.).
  • At compile-time, it lets you reuse the type of an existing variable.

Let;s see an example,

let word: string = "hello";

// Compile-time use (type copying)
let typeOfWord: typeof word;  // typeOfWord is of type 'string'

// Runtime use (type checking)
console.log(typeof word);     

Output

string

Here, typeof word copies the type string from the word variable and assigns it to typeOfWord.


typeof Types

At runtime, the typeof operator returns one of the following string values:

Types typeof Result
String "string"
Number "number"
BigInt "bigint"
Boolean "boolean"
Object "object"
Symbol "symbol"
undefined "undefined"
null "object"
function "function"

typeof Example

Example 1: typeof for String

const str: string = 'Hello';
console.log(typeof str); // string

Example 2: typeof for Number

const num: number = 42;
console.log(typeof num); // number

Example 3: typeof for Boolean

const flag: boolean = true;
console.log(typeof flag); // boolean

Example 4: typeof for Object

const obj: { name: string } = { name: 'Sam' };
console.log(typeof obj); // object

Example 5: typeof for Function

function greet(): void {}
console.log(typeof greet); // function

Uses of typeof operator

1. Debugging

You can use typeof to check the type of a variable, which helps with debugging or validating types at runtime.

let message: string = "Hello, World!";
console.log(typeof message); // string

2. In Type Definition

You can use typeof at compile-time to refer to the type of a variable in a type definition.

const user = { name: "John", age: 30 };
type UserType = typeof user;

let anotherUser: UserType = { name: "Alice", age: 25 };

3. In Conditional Statements

You can use typeof in conditional statements to perform different actions based on the type of a variable.

let value: string | number = "Hello";

if (typeof value === "string") {
  console.log("The value is a string");  // The value is a string
}
else if (typeof value === "number") {
  console.log("The value is a number"); 
}

More on TypeScript typeof Operator

Runtime and Compile-Time Use

Just like in JavaScript, typeof can be used at runtime to check the type of a value.

But TypeScript also adds a powerful feature — you can use typeof at compile time to refer to the type of a variable.

const age = 25;
console.log(typeof age); // number

type AgeType = typeof age;
// AgeType is now inferred as: number

Here, TypeScript shows how typeof checks the type at runtime and also extracts a variable's type at compile time.


Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges