TypeScript Data Types: Learn the Different Data types in TS

What are Data types in TypeScript?

Data types in TypeScript are used to tell the variables the type of data they can store.
TypeScript provides both built-in as well as user-defined data types.

Built-in Data Types

Here are the basic data types in TypeScript:


When a variable is declared without specifying a data type, it can be assigned a value of any type.

let sample: any = 143;

let sample: any = 143.341;

let sample: any = "Itsourcecode";

let sample: any = null;

let sample: any = undefined;

let sample: any = false;

In TypeScript, any type can hold any kind of value. It’s like turning off type-checking for a variable.


It is used to store multiple values in a single variable.

let grades: number[] = [75, 77, 80, 85, 90];
let grades: Array<number> = [90, 92, 93];


Represents true or false values.

let isDone: boolean = true;


It is used for defining named constants.

// Declare an enum
enum Website {IT, Source, Itsourcecode}

// Initialize a variable with an enum value
let Sample: Website = Website.Itsourcecode;

// Log the enum value to the console


number in TypeScript is a basic data type that represents numeric values and can be integer or floating point numbers.

let decimalExample: number = 42;
let hexExample: number = 0x2A;
let binaryExample: number = 0b101010;
let octalExample: number = 0o52;


string in TypeScript is a primitive data type that’s used to store text data. A TypeScript string operates with a sequence of characters.

let website: string = "ITSOURCECODE";


 It allows you to express an array with a fixed number of elements whose types are known, but need not be the same.

// Declare a tuple
let Sample: [string, number];

// Initialize the tuple
Sample = ["Itsourcecode", 8];

// Log the tuple to the console


It is used where a function does not return a value.

function greetUser(name: string): void {
    console.log("Hello, " + name + ". Welcome to Itsourcecode!");

// Call the function
greetUser("John Doe");

Null and undefined in TypeScript are special values, not types. You can assign them to variables, but they don’t describe the variable’s type.


Denotes value given to uninitialized variable

let variable: undefined = undefined;

Undefined means a variable has no value or object. On the other hand, null means a variable is set to an object, but the object’s value is undefined. They’re similar, but not the same.


It is used when an object does not have any value.

let variable: null = null;

What is User-defined Types?

User-defined types are types that you create yourself in your code. They’re not built into the language, but you make them to fit your needs.

Examples of user-defined types are classes, structures, unions, enums, and typedefs. These types let you make more complex data structures that are just right for your specific needs.

They can make your code easier to read and maintain by providing a higher level of abstraction. Remember, these types can be different in different programming languages.


In this article, we have learned about the different data types in TypeScript.

As we have discussed above, TypeScript has many data types that help programmers write better and error-free code.

I hope that this article helps you to have a better and more explicit understanding of what the TypeScript data types are.

If you have any questions or inquiries, please don’t hesitate to leave a comment below.

Leave a Comment