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:

Any

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.

Array

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];

Boolean

Represents true or false values.

let isDone: boolean = true;

Enum

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
console.log(Sample);

Number

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

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";
website= 'SOURCECODEHERO';

Tuple

 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
console.log(Sample);

Void

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.

Undefined

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.

Null

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.

Conclusion

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