EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials TypeScript Tutorial TypeScript Types

TypeScript Types

By Priya PedamkarPriya Pedamkar

TypeScript-Types

Introduction to TypeScript Types

The following article provides an outline for various types of TypeScript. Every Programming language is based on some keywords and datatypes. We have seen many types of data. The concept is so simple. We know that the alphabets are the one and numbers. For storing each type of data in proper way programming languages have separated them with some names.

Different Types of TypeScript

Don’t worry if you are new to these words. But if you are from a programming background then it is very easy to understand. In the coming section, we are looking at each of these in detail:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of TypeScript

1. Number

As we know TypeScript is a superset of JavaScript. So, Javascript doesn’t have data types like integer and float. Typescript belongs to the same concept. It has only one numeric type and i.e Number. But note that every number in typescript is a kind of floating-point.

Example

let marks: number;

We can also consider that for all types of no’s like float, double, int, long int, etc we have the only number as a data type.

2. String

As we’ve seen, the number datatype is just one like we have the string datatype. Any character or collection of characters is a string. This is one of the important data types we have in TypeScript. We can make use of both single quotes (‘ ‘) and double quotes(“ “). Template string is a new concept in Typescript.

Example

name: string = 'John';
Designation: string = "Engineer";

3. Enum

This is a very important one. It is more like an array. We can give a suitable name to the given values. Also, specify the index no.

Example

enum Fruits {apple = 2, banana, kiwi}
let favFruit: string = Fruits[3];

Look at the above code snippet. It has fruits and we decided to start its index at 2. Then the output will be banana.

4. Boolean

It is the simplest data type as we all know it gives only two values i.e. true and false.

Example

let isSuccess: boolean = true;

We are using this data type mostly on the condition-based rendering of the code.

5. Any

Any data type is very useful in programming at a time when we are not aware that what kind of data will be there. In many cases, we are unknown to the king of data being used. So, in these situations, we are giving it as Any. We can use this type of mixed array also.

Example

data: any;

When we are using a javascript framework like angular or react at that time we don’t know the type of data we get from a third party or an API. So at this instance of time, we can use any data type.

6. Void

Void is nothing but an empty thing. There is no data type for the given data. We are not using it on variables rather we use it on function as if the function does not have anything to return.

Example

function message(): void {
console.log("Welcome!!!");
}

7. Never

Like void never is also used on functions that never return anything or may go into the exception.

Example

Function abc() :never{……some code….}

Many times we have to handle the exception where code is not able to run or any error occurs. These functions are not returning any valuable data. In these scenarios, we can use never data type.

8. Array

Same like JavaScript, typescript also has array of data types. There are two ways to declare arrays in typescript. The array is nonprimitive or we can say reference type or user-defined data type. Every programming language has an array as its data type. It is considered as one of the important data types.

Example

let name:string[] = ['john','sam']
Letname: Array<string>= ['john','sam']

In the above example, we are specifying the type of array which is a string. In typescript, we mostly use angle brackets to specify the type.

9. Object

Array and object are reference types mainly i.e also known as user-defined or non-primitive.

Example

const obj = {}

10. Type assertions

Type assertion is nothing but type casting or type cohesion in other languages.

Example

let name: any = "Sam";
let nameNo: number = (<string> name).length;

In the above example, we have one variable called the name of type any.

On the second line as you see we have another variable nameno which is having a type number. Because we need string but need to get the length of our variable too.

11. Null and Undefined

These are the two most important and most confusing terms in typescript. These are also in javascript. If you are familiar with these then ok. If not don’t worry we will check this out.

  • Null: Null is we are using in a situation where we have to give value to the variable but not now. Maybe in the future. But today we have to give it some value in this situation we are giving null to that variable as a type.
  • Undefined: This is the data type that is by default get applied to all variables in javaScript. It is the same as void we can say.

Suppose we declared on variable and have not provided any value to it then by default it gets undefined as its datatype and value also.

12. Tuple

Tuple is nothing but the array which is having data of its data type with it.

Example

Let employee: [string, number];
employee= ["sam",101];

In the above example, we have an array named employee which will be having one value of type string and is of type number.

Conclusion

Typescript is mainly recognized for its strictly typing. JavaScript has dynamic typing which leads to some security issues. At this point, Typescript comes to save us from this drawback.

Recommended Articles

This has been a guide to TypeScript Types. Here we discuss the introduction and different types of TypeScript with examples. You may also have a look at the following articles to learn more –

  1. TypeScript Versions
  2. How to Install Typescript
  3. TypeScript Operators
  4. TypeScript Functions
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
Financial Analyst Masters Training Program
1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Data Science Bundle
1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access
All in One Software Development Bundle
3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
All in One Software Development Bundle3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access
Financial Analyst Masters Training Program1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

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

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
Let’s Get Started

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

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?

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

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

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW