TypeScript Typeof: Checking the Type of Objects & Variables in TS

What is typeof in TypeScript?

The typeof in TypeScript is a keyword that serves to distinguish among various types.

It enables us to discern differences among types such as numbers, strings, booleans, etc.

Syntax:

typeof variablename === "your_type"

Apart from that, we can directly use the typeof with the variable name we wanted to specify.

typeof variablename

It’s versatile and can be applied to any type, allowing for code reuse by accepting any parameter type.

The typeof operator can function as a Type guard in TypeScript, returning a string that signifies the type of a given value.

There are two forms in which typeof type guards can be recognized:

typeof t === "typename" 

and

typeof t !== "typename"

Where typename is one of the possible return values of typeof.

These return values could be undefined, number, string, boolean, object, or function.

How to use TypeScript typeof?

The typeof operator in TypeScript can be used in two contexts: as a value and as a type.

Here’s an illustration of how you can use it:

  1. As a Value:

The typeof can be used to get the type of a value in TypeScript.

Example 1:

let Samplevalue = "Hi, Welcome to Itsourcecode!";
console.log(typeof Samplevalue); 

Output:

string

Example 2:

let Samplevalue = 2024;
console.log(typeof Samplevalue); 

Output:

number

Example 3:

let bool = true;
console.log(typeof bool);

Output:

boolean

Example 4:

let obj = { name: "Code", age: 18 };
console.log(typeof obj); 

Output:

object

Example 5:

let undef;
console.log(typeof undef);

Output:

 undefined

Example 6:

let func = () => {};
console.log(typeof func); ✅  

Output:

function

  1. As a Type:

The typeof in TypeScript can also be used in a type context to refer to the type of a variable or property.

Example:

let x = "SourceCode";
let y: typeof s;  // y is now of type string ✅

y = "Itsourcecode";  // Assigning a string value to y
console.log(y);  

  1. Type Guard:

The typeof can be used as a type guard in TypeScript.

For example:

let SampleVar: any = "Hi, Welcome to Itsourcecode!";

if (typeof SampleVar === "string") { ✅
    // SampleVar is treated as "string" in this block

    console.log(SampleVar.toUpperCase());  

} else if (typeof SampleVar !== "string") { 
    // SampleVar is not treated as "string" in this block

    console.log("SampleVar is not a string.");
}

Output:

HI, WELCOME TO ITSOURCECODE!

How to Check the Type of Objects and Variables?

To check the type of objects and variables we can use the typeof Operator as we have already explained above

The typeof operator yields a string that represents the type of the operand.

It can be employed with objects to ascertain their type in TypeScript.

Example:

let variableName: any = "Hi, Welcome to Itsourcecode!";
console.log(typeof variableName);  

Output:

string

On the other hand, we can also use the instanceof Operator:

The instanceof operator is a binary operator utilized in object-oriented programming languages.

It verifies whether an object’s prototype chain contains the prototype property of a constructor.

Here’s an illustration of how you can use it:

class SampleClass { }
let myObject = new SampleClass();
console.log(myObject instanceof SampleClass);  

Output:

true

Conclusion

There you have it, we’re done discussing the typeof in Typescript which is a keyword that serves to distinguish among various types.

Understanding the typeof operator in TypeScript is crucial for effective type checking and ensuring code correctness.

I hope this article has helped you understand the TypeScript Typeof.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment