Updated March 20, 2023
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:
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 –