TypeScript Data Types

Types can be divided into 3 categories – AnyBuilt-in, and User-defined.

To assign a type in Typescript, you need a colon: then the name of the type, an equal sign =, and the value of that variable.

For example:

let name: String = "DotNetCrunch";

1. Any

This is the superset for all the data types available. It means that the variable could be of any type. It will override the type checking.

We need to define the type of the variable but at the time of writing the application, we might not be sure about the value. The value might come from dynamic content.

let dynamicValue: any = 4;

In the above example, I have just assigned 4 but the value might come dynamically and it could be a String or boolean instead of a number.

2. Built-in

The Built-in types include stringnumberbooleanundefinednull, and void.

String

String in TypeScript is similar to those in JavaScript. They are defined with single or double quotes.

let x: string = "Welcome to DotNetCrunch";

let y: string = 'Learn TypeScript Basics';

Number

The number represents the integer data type. It supports decimal, hexadecimal, octal, and binary literal.

let x: number = 0.789;
let y: number = 0xdnet;
let z: number = 0b0110;

Boolean

Boolean data type represents true or false similar to that of JavaScript.

let isAdmin: boolean = true;
let isError: boolean = false;

Undefined & Null

The undefined value is a primitive value used when a variable has not been assigned a value.

The null value is a primitive value that represents the null, empty, or non-existent reference.

TypeScript Data Types
source: Imgur

💡 As a best practice, you should always use undefined and not null.

Void

In programming languages like C# or Java, we use void as a return type for the function that doesn’t return anything. It works similarly in TypeScript. It is a subtype of undefined.

💡 The ‘void’ type can have undefined or null as a value whereas ‘never’ cannot have any value.

Never

It indicates that the value will never occur. It is used when you are sure that something is never going to happen.

👉 Read more on never

3. User-defined

The user-defined types include arrayenuminterfaceclassunion, and tuple.

Array

Arrays in TypeScript are similar to those in JavaScript. An array can be declared in two ways: using square brackets or using angle brackets.

Example:

let smiley: string[] = ['🙂','😃','🤣'];

OR

let smiley: Array<string> = ['🙂','😃','🤣'];

Enum

Enum is a way of giving more friendly names to the numeric values. They allow us to declare a set of named constants.

Example:

enum DayOfWeek { Mon, Tue, Wed, Thu, Fri, Sat, Sun }
let d: Day = DayOfWeek.Sat;

Interface

An interface defines how the data is being used. It is a way to define contacts within your code. We can define properties and methods inside an interface. In the TypeScript interface, arrow functions are also supported.

Example:

interface IStudent {
    studentId: number;
    studentName: string;
    getResult: (studentId: number) => number; // arrow function
    getBatchName(number): string; 
}

Class

TypeScript supports object-oriented programming. So, we can use classes and objects in typescript as we do in any other object-oriented programming language like C# or Java.

Example:

class Student {
    studentId: number;
    studentName: string;

    constructor(id: number, name: string) {
            this.studentId = id;
            this.studentName = name;
    }

    getResult(studentId: number) : number {
        // logic to calculate & return result...
    }
}

Union

In TypeScript, we can use more than one data type for a variable or a function. This data type is known as the Union.

let iD: string | number;
iD = 123; // OK
iD = "D007"; // OK
iD = true; // Compiler Error

Tuple

A tuple is a data type that holds two sets of values of different data types. In the example given below, we are storing both number and string type of data in the same variable.

Example:

let city: [number, string] = [1, "Mumbai"];