How To Define An Array Of Strings In Typescript

Define an Array of Strings in TypeScript

Learning about define an array of strings in Typescript is fundamental if you want to work with an array in Typescript. So how to do it? Let’s go into detail now.

Define an array of strings in TypeScript

Use Array <T> syntax

We can use Array <T> syntax and pass string type inside to declare an array with that type.

Example:

const animalList: Array<string> = [
    "Eastern Lowland Gorillas",
    "Elephant Shrew",
    "Fiji Blue Devil Damsel Fish",
    "Fiordland Crested Penguin",
    "Scottish Crossbill",
];

Use T[] syntax

With T[] syntax, the T is set for the type of element inside the Array.

Example:

const animalList: string[] = [
    "Eastern Lowland Gorillas",
    "Elephant Shrew",
    "Fiji Blue Devil Damsel Fish",
    "Fiordland Crested Penguin",
    "Scottish Crossbill",
];

Here with the two syntaxes above, I declare a string array that can only get the string element. I will get the error if I pass in any type that is not a string type.

Example:

const animalList : string[] = ['Eastern Lowland Gorillas', 'Elephant Shrew', 0]

The error:

Type' number' is not assignable to type 'string'

Here I get an error because I pass in the string array a number element which is 0.

Besides string array, you can also pass in any type to make your Array become that typed Array.

Example:

// Number array
const numberList: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Object array
const objList: object[] = [{ greeting: "Nice to meet you" }];

type detailPerson = {
    name: string;
    age: number;
    country: string;
};

// Array with specific type element
const personList: detailPerson[] = [
    { name: "James", age: 49, country: "England" },
    { name: "Joma", age: 39, country: "Italy" },
];

With a string array, each element in the Array is a string, so you can work with it or using string method like ordinary.

Example:

const animalList: string[] = [
    "Eastern Lowland Gorillas",
    "Elephant Shrew",
    "Fiji Blue Devil Damsel Fish",
    "Fiordland Crested Penguin",
    "Scottish Crossbill",
];
  
// Loop over all array and log the first character of each string
animalList.forEach((ele) => {
    console.log(ele.charAt(0));
});

Output:

[LOG]: "E" 
[LOG]: "E" 
[LOG]: "F" 
[LOG]: "F" 
[LOG]: "S"

Here with each Array element, I use the charAt string method to get the letter at the first position of each string element.

Use union type

If you want to make your Array can use another type besides string type, you can use union type.

Example:

const anArray: Array<string|number> = ['one', 'two', 'three', 4 ,5 ,6 ,7 ,8 ,9 ,10]
console.log(anArray)

Output:

[LOG]: ["one", "two", "three", 4, 5, 6, 7, 8, 9, 10]

Here I declare an array that can get string and number type elements. You can also set multiple types for Array’s element to follow your purpose and separate it with the ‘|’ symbol.

Summary

In this article, I showed you how to define an array of strings in Typescript. You can use Array <T> or T[] syntax after the colon symbol to declare an array with that type. Let’s try these methods to get your desired results.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *