Typescript data structures

Created By: chatGPT

In TypeScript, data structures can be represented using various built-in types such as arrays, tuples, enums, sets, and maps. Understanding how to utilize these data structures effectively is essential for building efficient applications. Below are some examples of these key data structures.
Arrays in TypeScript are used to store multiple values in a single variable. They are defined using square brackets and can hold any type of data.
let numbers: number[] = [1, 2, 3, 4, 5];
let fruits: string[] = ['apple', 'banana', 'cherry'];
A tuple is a special type of array that allows you to define an array with a fixed number of elements, where each element can have a different type. You can use tuples to hold a specific set of related values.
let user: [string, number] = ['Alice', 30];
Enums are a way to define a set of named constants. Enums improve code readability and make it easier to manage sets of related values.
enum Color {
    Red,
    Green,
    Blue
}
let c: Color = Color.Green;
Sets are collections of unique values. In TypeScript, you can use the Set object to store and manage these unique values efficiently.
let uniqueNumbers: Set<number> = new Set();
uniqueNumbers.add(1);
uniqueNumbers.add(2);
uniqueNumbers.add(2); // Duplicate, will not be added.
Maps are used to hold key-value pairs. They are similar to objects but allow any data type to be used as a key, providing more flexibility and improving data management.
let map: Map<string, number> = new Map();
map.set('apple', 1);
map.set('banana', 2);
let appleCount = map.get('apple'); // Returns 1
Combining these data structures allows developers to create complex data models. For instance, you might create an array of objects representing users, where each user contains properties defined by an interface.
interface User {
    username: string;
    age: number;
}

let users: User[] = [
    { username: 'Alice', age: 30 },
    { username: 'Bob', age: 25 }
];
Introduction And SetupVariablesData TypesAnyUnknownVoidNeverStringNumberBooleanArrayTupleEnumObjectInterfaceType AliasMethodsFunctionArrow FunctionReturn TypeParametersDefault ParametersOptional ParametersRest ParametersControl FlowIf StatementElse StatementSwitch StatementFor LoopWhile LoopDo While LoopFor...of LoopFor...in LoopBreak StatementContinue StatementFunctionsFunction OverloadingRecursive FunctionExpression FunctionOperatorsArithmetic OperatorsAssignment OperatorsComparison OperatorsLogical OperatorsBitwise OperatorsConditional (ternary) OperatorData StructuresArrayTupleObjectMapSetCommentsSingle Line CommentMulti Line CommentSyntaxType AnnotationsType InferenceNamespaceModuleExportImportDecoratorsAmbient DeclarationsEvent HandlingEvent ListenersAddEventListenerRemoveEventListenerError HandlingTry...catch StatementThrow StatementFinally BlockCustom Error TypesAsync AwaitPromisesGenericsConditional TypesMapped TypesIntersection TypesUnion TypesLiteral TypesDiscriminated UnionsType GuardsTypeof GuardInstanceof GuardAssertion FunctionsModule Augmentation