Typescript
Menu
Datatypes
- TypeScript provides a rich set of data types that allow developers to specify the type of data a variable can hold.
- This feature improves code quality and maintainability by catching compile-time errors.
- Below are the primary data types in TypeScript, explained in detail with coding examples.
Number
- Description: Represents both integers and floating-point numbers.
- Example:
let age: number = 40; // Integer
let price: number = 29.99; // Floating-point
let hex: number = 0xf00d; // Hexadecimal
let binary: number = 0b1010; // Binary
let octal: number = 0o744; // Octal
String
- Description: Used for text data, it can be enclosed in single quotes, double quotes or backslashes (template literals).
- Example
let name: string = "John Doe";
let greeting: string = `Hello, ${name}!`; // Template literal
Boolean
- Description: Represents a boolean value that can be true or false.
- Example:
let isActive: boolean = true;
Null
- Description: Represents the intentional absence of any object value.
- Example:
let user: null = null;
Undefined
- Description: Indicates the value assigned to an uninitialized variable.
- Example:
let uninitialized: undefined;
Any
- Description: A flexible type that allows any value and effectively disables type checking for this variable.
- Example:
let variable: any = "Hello";
variable = 50; // Still valid
Unknown
- Description: A safer alternative to everything where you can’t perform operations on it until you’ve type checked it.
- Example:
let value: unknown = 5;
// console.log(value.toFixed(2)); // Error: Object is of type 'unknown'
if (typeof value === "number") {
console.log(value.toFixed(2)); // Now it's safe to use
}
Void
- Description: Used for functions that do not return a value.
- Example:
function logMessage(message: string): void {
console.log(message);
}
Never
- Description: Represents a type that never occurs, such as a function that always throws an error or a function with an infinite loop.
- Example:
function throwError(message: string): never {
throw new Error(message);
}
function infiniteLoop(): never {
while (true) {}
}
Object
- Description: Represents a non-primitive type that can contain various properties.
- Example:
let person: object = {
name: "John",
age: 25,
isActive: true,
};
Array
- Description: It can contain a collection of values of a specific type.
- Example:
let numbers: number[] = [1, 2, 3, 4];
let strings: Array<string> = ["apple", "banana", "cherry"];
Tuple
- Description: Represents an array with a fixed number of elements, each of which can be of a different type.
- Example:
let tuple: [string, number] = ["John Doe", 20];
Enum
- Description: A method of assigning descriptive names to a set of numeric values.
- Example:
enum Courses { TypeScript, Ionic, Angular2, NodeJS };
let tscourse: Courses = Courses.TypeScript;
console.log(tscourse); // Outputs: 0 (the index of TypeScript)
Conclusion
- Understanding these data types is critical to writing effective TypeScript code.
- By leveraging static typing and various built-in types, developers can create more reliable and maintainable applications while catching bugs early in the development process.