In TypeScript, data types are used to specify the types of values that can be assigned to variables or used as function parameters and return values. TypeScript is a statically typed language, which means the types of values are determined at compile-time. This allows the TypeScript compiler to catch type-related errors early on and improves code readability.
Here is a list of basic data types provided by TypeScript:
1. Boolean
The Boolean data type represents a true or false value. For example:
let isActive: boolean = true;
let isInactive: boolean = false;
2. Number
The Number data type represents both integer and floating-point values. TypeScript uses the floating-point number format defined by the IEEE 754 standard. For example:
let integer: number = 10;
let floatingPoint: number = 3.14;
3. String
The String data type represents a series of Unicode characters. In TypeScript, you can use single or double quotes (or even backticks for template literals) to define string literals. For example:
let companyName: string = 'TypeScript Inc.';
let website: string = "https://www.typescriptlang.org/";
let welcomeMessage: string = `Hello, welcome to ${companyName}!`;
4. Array
Arrays are collections of elements of the same type. In TypeScript, you can define arrays in two ways, either by specifying the ‘Array<Type>‘ syntax or using the ‘Type[]‘ syntax. For example:
let numbers1: number[] = [1, 2, 3, 4, 5];
let numbers2: Array<number> = [6, 7, 8, 9, 10];
let names: string[] = ['Alice', 'Bob', 'Cathy'];
5. Tuple
Tuples in TypeScript allow you to represent a fixed-size array where each element can have a different type. For example:
let person: [string, number] = ['Alice', 30];
6. Enum
Enums provide a way of giving friendly names to sets of numeric values. For example:
enum Color {
Red,
Green,
Blue,
}
let primaryColor: Color = Color.Red;
7. Any
The Any data type is useful when you don’t know the type of value you are working with. It allows you to assign any type of value to a variable. For example:
let data: any = 'Hello';
data = 42;
data = [1, 2, 3];
8. Void
The Void data type is the absence of having any type at all. It is commonly used as the return type of functions that do not return a value. For example:
function printMessage(message: string): void {
console.log(message);
}
9. Null and Undefined
Null and Undefined are subtypes of all other types in TypeScript. Their values can be assigned to any type of variable. For example:
let nothing: null = null;
let absence: undefined = undefined;
10. Never
The Never data type is used for values that never occur, for instance, in functions that always throw an error or have an infinite loop. For example:
function throwError(message: string): never {
throw new Error(message);
}
function infiniteLoop(): never {
while (true) {}
}
11. Object
The Object data type represents non-primitive types in TypeScript. It can be used to represent any object type value, except for primitive types (number, string, boolean, null, undefined, symbol). For example:
let person: object = { name: 'Alice', age: 30 };
let animals: object[] = [{ name: 'dog' }, { name: 'cat' }, { name: 'fish' }];
These are some of the basic data types in TypeScript that you may encounter while using the language. There are also advanced type features like union types, intersection types, type aliases, and more which provide additional flexibility and control over your TypeScript code.