Types - Array, Tuple, Enum

  1. TS support for arrays

    The array type is written as array name: data type in array[], such as hobbies: string[]. Using arrays inside and outside objects is exactly the same.

    Another way to write it is Array<type>, for example hobbies: Array<string>. These two ways of writing are equivalent.

    TS will also automatically infer the array type if an initial value is assigned to the array (instead of an empty array). like:

    1
    2
    3
    4
    const person = {
    name: "Derek",
    hobbies: ["Sport", "Cooking"] //TS will infer the type as hobbies: string[]
    };

  2. Arrays in TS cannot be of mixed types

    In TS, if a type is defined for an array, elements of other types cannot be added to it. If you want an array to contain elements of different types, the solutions are:

    1. Assign any type to the array. any is flexible, but gives up all the benefits provided by TS.

      1
      2
      let activities: any[];
      activities = ["Sports", 1];
    2. Tuple

    3. Union type array


  3. Tuple Tuple

    Tuples are fixed-length and fixed-type arrays. Tuples are added by TS. The type is written as role: [number, string];.

    1
    2
    3
    4
    5
    const person = {
    name: "Derek",
    hobbies: ["Sport", "Cooking"],
    role: [2, "author"] //Tuple, TS inferred type is role:(string | number)[], which is a union type, not a tuple
    };

    You can see that the result of TS inference is a union type array, not a tuple. The length of this kind of array is not fixed, you can continue to add elements to it, modify the type of an element, etc. So tuple types need to be explicitly set.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    const person: {
    name: string;
    hobbies: string;
    role: [number, string]; //How to write tuple type.
    } = {
    name: "Derek",
    hobbies: ["Sport", "Cooking"],
    role: [2, "author"]
    };

    The assignment to each positional element in the tuple is only the type declared by the tuple. For example, in the above tuple, if the assignment

    1
    2
    3
    4
    person.role[0] = "Derek"; //Error report
    person.role[1] = 10; //error report
    person.role = [0, "admin", "user"]; //Error report
    person.role.push("user"); //No error reported

    The first three are wrong. But push() is not restricted by tuples.


  4. Enum enumeration

    Enum convention begins with a capital letter. Because enumeration is also a custom type. An enumeration lists all possible values of a variable.

    1
    enum Role { ADMIN, READ_ONLY, AUTHOR };

    Enum automatically identifies values as numbers. The default number is the index of the value in the array. When the mouse hovers over the enum value, (enum member) Role.AUTHOR = 2 will be displayed.

    You can also not use the default numeric identifier and assign it manually:

    1
    enum Role { ADMIN = 5, READ_ONLY = 100, AUTHOR = 200};

    If only the first value is manually assigned a numeric ID, subsequent values are automatically assigned increasing numeric IDs. Manually assigned IDs can also be non-numeric. Can also be mixed. like:

    1
    enum Role { ADMIN = 'my_admin', READ_ONLY = 100, AUTHOR = 200};

    Usage when using enumeration values is:

    1
    let role = Role.ADMIN; //The identification value of the tuple value will be obtained, such as my_admin.

Share