EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript typeof

TypeScript typeof

Shalu Sharma
Article byShalu Sharma
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated July 5, 2023

TypeScript typeof

Definition of TypeScript typeof

typeof in TypeScript has the same name as JavaScript but functions differently. typeof is used to differentiate between the different types in TypeScript. By the use of typeof we can differentiate between number, string, symbol, Boolean, etc. typeof can be used with any type in TypeScript; by the use of it, we can re-use the code by passing any parameter type. In the coming section, we will see more about the typeof in TypeScript to understand it better.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

As we know, typeof is a keyword that is used to deal with different types in TypeScript. Let’s see its syntax in detail for better understanding. See below;

typeof variable === "your_type"

As you can see in the above line of syntax, we can directly use this typeof with the variable name which we want to calculate. After this, we can write our own logic as per the requirement. In the coming section of the article, we will discuss this usage in detail for better understanding.

How does typeof work in TypeScript?

As we already know that typeof is used to deal with several types of typeof in TypeScript. This allows us to help us with the various type in TypeScript; we can reuse the same code for multiple types in TypeScript. In this section, we will see how to use this while programming in TypeScript. Let’s see its usage and understand it better for future use. See below;

Keyword usage:

typeof variable_name //

In the above lines of code, we are using typeof keyword with the variable. It will tell us the type of variable. After this, we can use this to perform any operation on it. Now see one sample example for typeof in TypeScript for beginners to understand its internal working. See below;

e.g. :

class Demo{
testFunction(val: any): any{
if(typeof val === "string"){
// your logic will go here ..
return val;
}
}
}
let demoobj = new Demo();
let result= demoobj.testFunction("i am string !!");
console.log(result);

In the above example, we are creating one function which accepts any type of parameter. Inside the ‘Demo’ class, we have created one function named ‘testFunction’. Inside this function, we are using typeof keyword to compare the type of the variable being passed. typeof will return us the type of the parameter. This is similar to the instanceOf keyword already available. After the comparison, we can write our own logic as per the requirement. At the last to call the function in TypeScript we are creating an object of the class and calling the function. This function we have created takes one parameter; this parameter can be of any type because we have assigned the type as ‘any’ in the function declaration. This is how it works in TypeScript, like any other programming language. It has some benefits as well. Let’s discuss them each one by one;

1) By using typeof in TypeScript, we can easily make checks which can help us to get any exception further exceptions if we try to cast them incorrectly.

2) But by making small checks, we can reuse the code for any different type in TypeScript.

Examples

In this example, we are trying to use the typeof with numbers in Typescript; if the passes parameter matches with the string type, then it will return the value; otherwise, nothing will be printed.

Example #1

Code:

class Demo{
testFunction(val: any): any{
if(typeof val === "string"){
return val;
}
}
}
console.log("Demo to show the usage of typeof in Typescript !!");
let demoobj = new Demo();
let result1 = demoobj.testFunction("i am string !!");
let result2 = demoobj.testFunction("i am string two !!");
let result3 = demoobj.testFunction("i am string three !!");
console.log("printing result of the function after typeof !!");
console.log("result one is::" +result1);
console.log("result two is::" +result2);
console.log("result three is::" +result3);

Output:

Example 1

Example #2

In this example, we are trying to use the typeof with numbers in Typescript; if the passes parameter matches with the number type, then it will return the value; otherwise, nothing will be printed.

Code:

class Demo{
testFunction(val: any): any{
if(typeof val === "number"){
return val;
}
}
}
console.log("Demo to show the usage of typeof in Typescript !!");
let demoobj = new Demo();
let result1 = demoobj.testFunction(100);
let result2 = demoobj.testFunction(400);
let result3 = demoobj.testFunction(500);
let result4 = demoobj.testFunction(500);
let result5 = demoobj.testFunction(500);
console.log("printing result of the function after typeof !!");
console.log("result one is::" +result1);
console.log("result two is::" +result2);
console.log("result three is::" +result3);
console.log("result four is::" +result4);
console.log("result five is::" +result5);

Output:

Example 2

Example #3

In this example, we are trying to use the typeof with numbers in Typescript; if the passes parameter matches with the boolean type, then it will return the value; otherwise, nothing will be printed.

Code:

class Demo{
testFunction(val: any): any{
if(typeof val === "boolean"){
return val;
}
}
}
console.log("Demo to show the usage of typeof in Typescript !!");
let demoobj = new Demo();
let result1 = demoobj.testFunction(true);
let result2 = demoobj.testFunction(false);
let result3 = demoobj.testFunction(true);
let result4 = demoobj.testFunction(false);
let result5 = demoobj.testFunction(true);
console.log("printing result of the function after typeof !!");
console.log("result one is::" +result1);
console.log("result two is::" +result2);
console.log("result three is::" +result3);
console.log("result four is::" +result4);
console.log("result five is::" +result5);

Output:

TypeScript typeof 3

Conclusion

In the typeof keyword easy to use and understand. We can easily check the type of the parameter by simply using the typeof keyword before the variable name. This is very handy to use with variables in TypeScript. We can compare integers, numbers, Boolean, symbols, undefined, strings, objects, etc., by using typeof in Typescript.

Recommended Articles

We hope that this EDUCBA information on “TypeScript typeof” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. TypeScript Functions
  2. TypeScript Array
  3. TypeScript Operators
  4. TypeScript Types
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Blog as Guest
Courses
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW