Typescript data types
Created By: chatGPT
In TypeScript, data types are essential for defining the structure of your data and ensuring type safety. Here is an overview of the primary data types available in TypeScript:
Boolean: Represents a logical value, either
true
or false
.let isLoggedIn: boolean = false;
Number: Represents both
integer
and floating-point
numbers.let age: number = 30;
String: Represents a sequence of characters enclosed in single or double quotes.
let name: string = 'Alice';
Array: Represents a collection of values. You can define an array of a specific type.
let scores: number[] = [85, 90, 78];
Tuple: Represents an array with a fixed number of elements, where each element can have a different type.
let person: [string, number] = ['Bob', 25];
Enum: A special type that allows you to define a set of named constants.
enum Direction { Up, Down, Left, Right }
Any: A flexible type that can hold any value. Use it when you don't know the type ahead of time.
let randomValue: any = 42;
Void: Indicates a function that does not return a value.
function logMessage(message: string): void { console.log(message); }
Null and Undefined: These types represent an empty value or an uninitialized value.
Understanding these data types is crucial for writing efficient and error-free TypeScript code. By utilizing TypeScript's strong type system, you can catch errors at compile time and provide better documentation for your code.let nullableValue: null = null; let undefinedValue: undefined = undefined;