Typescript Arrays can hold mixed value types

Arrays can hold mixed value types

Before we learn how to create a strongly-typed array, let’s look at an array that has been declared without a type annotation. The code below constructs an array containing three different values, each with a different type.

const items = [];
items.push(1);
items.push("two"),
items.push(false);

console.log(items);

Using the Array generic type

We can use a type annotation to specify that an array should only contain items of a specific type. There is an Array generic type that we can use to do this. We pass the type we want the items to have in angle brackets after the word Array.

More articles :

Reddit web hosting reviews 

Leave a Reply

Your email address will not be published.