Data types in typescript with examples

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:

  1. 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.

TypesDescriptionType annotation
BooleanThe boolean type in TypeScript accepts two values: true and false. It is one of TypeScript’s primitive types.var a :boolean
StringThese textual datatypes are referred to by the type string. TypeScript, like JavaScript, surrounds string data with double quotes (“) or single quotes (‘).var a : string
NumberIn 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
ObjectAll 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’ };
ArrayYou 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>
TupleA 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]
EnumA 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, …};
AnySome 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
NeverThe 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);
}
UnionIn 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;
UnknownIn 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 anythinglet a: unknown
What are the data types in typescript?

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
What are the data types of typescript
What are the data types of typescript

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
string data types of typescript
string data types of typescript

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
Number data type in typescript
Number data type in typescript

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.

Object type in typescript
Object type in typescript

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
array type in typescript
array type in typescript

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
tuple type in typescript
tuple type in typescript

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
Enum type in typescript
Enum type in typescript

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
Any type in typescript
Any type in typescript

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 
Data types in typescript with examples
Data types in typescript with examples

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:

>