Typescript

Array

  • Arrays in TypeScript are a powerful feature that allows you to store multiple values ​​in a single variable.
  • They can contain elements of a specific type or mixed types and come with various built-in manipulation methods.
  • Below are the key aspects of fields in TypeScript, explained in bullet points with coding examples.
  1. Declaration of Arrays

    • Using Square Brackets: You can declare an array using the square brackets [] syntax.
      let fruits: string[] = ['Apple', 'Banana', 'Cherry'];
    • Using Generic Array Type: Alternatively, you can use the Array<elementType> syntax.
      let fruits: Array<string> = ['Apple', 'Banana', 'Cherry'];
  2. Initialization of Arrays

    • Arrays can be declared and initialized simultaneously:
      let numbers: number[] = [1, 2, 3, 4];
    • You can also declare an array without initialization and assign values later:
      let colors: string[];
      colors = ['Red', 'Green', 'Blue'];
  3. Mixed Data Types

    • TypeScript allows arrays to contain elements of different types using union types.
      let mixedArray: (string | number)[] = ['Alice', 25, 'Bob', 30];
  4. Accessing Array Elements:

    • You can access elements using their index (zero-based).
      let firstFruit = fruits[0]; // 'Apple'
  5. Modifying Arrays

    • You can add elements to an array using the push() method.
      fruits.push('Mango'); // Adds 'Mango' to the end of the array
    • You can remove elements using the pop() method.
      let lastFruit = fruits.pop(); // Removes and returns the last element
  6. Array Properties and Methods

    • Length Property: The length property returns the number of elements in an array.
      console.log(fruits.length); // Outputs: number of elements in fruits array
    • Common Methods:
      • forEach(): Executes a provided function once for each array element.
        fruits.forEach(fruit => console.log(fruit));
      • map(): Creates a new array with the results of calling a provided function on every element.
        let upperCaseFruits = fruits.map(fruit => fruit.toUpperCase());
      • filter(): Creates a new array with all elements that pass the test implemented by the provided function.
        let longFruits = fruits.filter(fruit => fruit.length > 5);
  7. Readonly Arrays

    • You can create arrays that cannot be modified using the readonly modifier.
      const readonlyFruits: readonly string[] = ['Apple', 'Banana'];
      //readonlyFruits.push('Cherry'); // Error: Property 'push' does not exist on type 'readonly string[]'.
  8. Array Type Inference

    • TypeScript can infer the type of an array based on its initial values.
      const numbers = [1, 2, 3]; // Inferred as number[]
      numbers.push(4); // No error
      // numbers.push("5"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.

 

Conclusion

  • TypeScript arrays are versatile and provide powerful typing capabilities that improve code quality and maintainability.
  • By understanding how to declare, initialize, access, and manipulate arrays, you can use them to their full potential in your TypeScript applications.