In this typescript tutorial, we will see what the different basic data types are available in typescript, which we can use while programming. As we know typescript is the superset of javascript, so typescript will help you to validate which type of values we are using, which ensures that the program runs as expected. Let us read, various data types in typescript with examples.
Now, in this tutorial we will cover:
- What are the data types of typescript
- How do you declare data types in TypeScript?
- What are the data types in typescript
- Data types in typescript with examples
What are the data types of typescript
When a variable is defined, the objective is to allocate some value to that variable, but the type of value that may be allocated to that variable is determined by its datatype. This type of system in typescript represents the many datatypes that TypeScript supports.
Here are the data types supported in typescript:
- Boolean
- String
- Number
- Object
- Array
- Tuple
- Enum
- Any
- Never
- Union
- Unknown
Read Typescript hello world program for beginners
How do you declare data types in TypeScript?
Here we will see how we can declare data types in typescript.
In TypeScript, the type syntax for declaring a variable is a colon (:) followed by the variable’s type. To declare a variable, we use the var keyword in the same way as we do in Javascript.
We can declare a variable with types in four ways:
- Declare both its type and value in a single sentence.
var [identifier]: type annotation = value
2. Declare the type but not the value. The variable will be assigned to undefined in this situation.
var [identifier]: type annotation;
3. Declare the value but not the type. The data type of the assigned value will be set as the variable type.
var [identifier]= value
4. Declare neither the value nor the type. In this situation, the variable’s data type will be any and it will be set to undefine.
var identifier
What are the data types in typescript?
Here we will see what are the data types in typescript.
Now let’s deep dive into different data types available in typescript.
Types | Description | Type annotation |
---|---|---|
Boolean | The boolean type in TypeScript accepts two values: true and false. It is one of TypeScript’s primitive types. | var a :boolean |
String | These textual datatypes are referred to by the type string. TypeScript, like JavaScript, surrounds string data with double quotes (“) or single quotes (‘). | var a : string |
Number | In TypeScript, all numbers are either floating-point values or big integers. The type number is assigned to floating-point numbers, while big integers are assigned the type bigint. | var a: number |
Object | All values that are not primitive types are represented by the TypeScript object type. The primitive types are integer, string, boolean, bigint, symbol, null, or undefined. | let xyz: object; xyz= { firstName: ‘John’, lastName: ‘jill’, age: 29, jobTitle: ‘Web Developer’ }; |
Array | You can work with arrays of values in TypeScript just as in JavaScript. There are two different approaches to write array types. When referring to an array of that element type in the first, you use the element type followed by [] | 1. let list: type[] 2. let list: Array<number> |
Tuple | A tuple is similar to an array with the following exceptions: The tuple has a fixed number of elements. The items’ kinds are known and do not have to be the same. | let skill:[string] |
Enum | A set of named constant values makes up an enum. The term “enumerated type” is “enum.” The steps to define an enum are as follows: Use the enum keyword first, then the enum’s name. Create the enum’s constant values after that. | enum name {constant1, constant2, …}; |
Any | Some times, certain types of information are unavailable or their disclosure would require an excessive amount of work. In certain circumstances, type checking may not be appropriate, to do so we can use the any type. | let a: any |
Never | The type never is a type with no values. As a result, you are unable to set any value to a variable that has a never type. The never type is often used to describe the return type of a function that always returns an error. | function error(message: string): never { throw new Error(message); } |
Union | In typescript Union type is used when you will come across a function that expects a number or a string as an argument. | let a: number | string; |
Unknown | In typescript we will use the type when a user don’t know the type of the data. So we need to define the variable as unknown type, so we can tell the compiler and user that the variable can be anything | let a: unknown |
Read Typescript Identifiers and Keywords
Data types in typescript with examples
Here we will see an example of data types supported in typescript.
Now let’s see an example for each type and how we can define types in typescript.
Example 1: Boolean type in typescript
As we know boolean type variable contains simple true/false values, so let’s see with an example.
Open a code editor, and create a new file with the name, ‘typeExample.ts’ file, write the below code:
let isActive: boolean = false;
console.log (isActive);
To compile the code and run the below command, you can see the result in the console.
ts-node typeExample.ts

Example 2: String data type in typescript
Now we will see an example of how we can declare string data types in typescript. In the typeExample.ts file write the below code:
let country: string= "USA"
console.log(country);
To compile the code and run the below command, you can see the result in the console.
ts-node typeExample.ts

Example 3: Number data type in typescript
Now we will see how we can declare a number data type in typescript. In the typeExample.ts file write the below code.
let num:Number= 20
console.log(num)
To compile the code, run the below command and you can see the result in the console.
ts-node typeExample.ts

Example 4: Object type in typescript
Now we will see how we can declare an object type in typescript, as we know object type is a nonprimitive type. So, in the below example, you can see how we can declare object type in typescript. In the typeExample.ts file write the below code.
const employee: { id: number, name: string, dob: string } = {
id: 1,
name: "Alex",
dob: "11/12/2001"
};
console.log(employee);
To compile the code and run the below command, you can see the result in the console.

Read Typescript type annotation
Example 5: Array type in typescript
Now we will see how we can declare an array type as a string in typescript. In the typeExample.ts file, write the below code:
let names:Array<string>= ["Alex","Ketty","Ruby","John"];
console.log(names);
To compile the code and run the below command and you can see the result in the console:
ts-node typeExample.ts

Example 6: Tupled type in typescript
Now we will see how we can use tupled type in typescript. To use tupled in typescript, we need to provide the type of each item in the array. In the typeExample.ts file, write the below code:
let info:[number, boolean, string];
info= [1, true,"Approved"]
console.log(info);
To compile, the code and run the below command, you can see the result in the console.
ts-node typeExample.ts

Example 7: Enum type in typescript
Now we will see how we can define Enum type in typescript.
As we know an enum is a kind of “class” that represents a collection of constants (unchangeable variables). String and numeric enums are the two types of enums.
In the typeExample.ts write the below code: Enums contains
enum Directions {
North = 'North',
East = "East",
South = "South",
West = "West"
};
console.log(Directions);
To compile the code and run the below command, you can see the result in the console.
ts-node typeExample.ts

Example 8: Any type in typescript
Now we will see an example of how we can declare any type in typescript.
In TypeScript, any is a data type. When dealing with third-party applications and expecting any variable but not knowing the specific type of variable, we utilize any type.
Any data type is utilized because it facilitates opting in and out of type checking during compilation.
In the typeExample.ts file, write the below code:
let num : any[]=[ 1, "xyz", 2];
num.push("normal");
console.log(num);
To compile the code and run the below command, and you can see the output in the console.
ts-node typeExample.ts

Read How to check if string is empty in typescript
Example 9: Union type in typescript
Here we will see an example of how we can use union type in typescript.
TypeScript allows us to utilize many data types for variables or function parameters. This is known as the union type.
In the typeExample.ts file write the below code:
let val:(string|number);
val=111
console.log(val);
val="xyz"
console.log(val);
To compile the code and run the below command, you can see the result in the console.
ts-node typeExample.ts

This is how we can use different data types of typescripts.
Conclusion
In this typescript tutorial, we saw what is data types in typescript, how to declare types in typescript, and how we can use types in our code by typescript.
- What are the data types of typescript?
- How do you declare data types in TypeScript?
- What are the data types in typescript
- Data types in typescript with examples
You may also like the following typescript tutorials:
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com