Declare an Empty Array for a typed Variable in TypeScript

avatar
Borislav Hadzhiev

Last updated: Jan 20, 2023
3 min

banner

# Declare an Empty Array for a typed Variable in TypeScript

To declare an empty array for a typed variable, set the array's type to Type[].

Any elements you add to the array need to conform to the specified type, otherwise, an error is raised.

index.ts
type Animal = { name: string; age: number; }; const arr: Animal[] = []; const a1: Animal = { name: 'Alfred', age: 2 }; arr.push(a1); // ๐Ÿ‘‡๏ธ [{name: 'Alfred', age: 2}] console.log(arr);

declare empty array for typed variable

The code for this article is available on GitHub

We declared an arr variable and set it to an empty array of objects of type Animal[].

The same approach can be used to declare an empty array of primitives.

index.ts
const arr: string[] = []; console.log(arr); arr.push('bobby', 'hadz', 'com'); // ๐Ÿ‘‡๏ธ ['bobby', 'hadz', 'com'] console.log(arr);

The arr variable can only store values of type string.

We can only add elements of the specified type to the array, otherwise, an error is raised.

index.ts
type Animal = { name: string; age: number; }; const arr: Animal[] = []; // โ›”๏ธ Error: Argument of type {test: string} is not // assignable to parameter of type 'Animal' arr.push({ test: 'hello' });

The object we tried to push into the array doesn't conform to the Animal type, so TypeScript errors out.

TypeScript also warns you if you forget to add a property that is required on the type.

index.ts
type Animal = { name: string; age: number; }; const arr: Animal[] = []; // โ›”๏ธ Error: Property of type 'age' is missing // but required in type Animal arr.push({ name: 'Alfred' });

You can use the any type if you don't want to explicitly type the array.

index.ts
const arr: any[] = []; arr.push('bobby'); arr.push(30); arr.push({ country: 'Chile' }); // ๐Ÿ‘‡๏ธ [ 'bobby', 30, { country: 'Chile' } ] console.log(arr);

The any type effectively turns off type checking and should be used sparingly.

If you need to filter an array of objects in TypeScript, check out the following article.

# Declare an Empty Array for a typed Variable using a type assertion

Alternatively, you can use a type assertion to declare an empty typed array.

index.ts
type Animal = { name: string; age: number; }; const arr = [] as Animal[]; const a1: Animal = { name: 'Alfred', age: 2 }; arr.push(a1); // ๐Ÿ‘‡๏ธ [{name: 'Alfred', age: 2}] console.log(arr);

declare empty array for typed variable using type assertion

The code for this article is available on GitHub

When using type assertions, we effectively tell TypeScript that we know better and the arr variable is definitely of type Animal[].

Type assertions are often used when fetching data from a remote source like a database because TypeScript likely doesn't know what type of data you fetch with an API request.

When using a type assertion, we would also get an error when we try to push a value that doesn't conform to the type into the array.

index.ts
type Animal = { name: string; age: number; }; const arr = [] as Animal[]; // โ›”๏ธ Error: Property of type 'age' is missing // but required in type Animal arr.push({ name: 'Alfred' }); // โ›”๏ธ Error: Argument of type {test: string} // is not assignable to parameter of type Animal arr.push({ test: 'hello' });

The first call to the push() method causes an error because the age property is missing on the object.

The second call causes an error because the supplied object is not of type Animal.

Which approach you pick is a matter of personal preference. In this situation I'd go with the first approach as there is no real need for a type assertion.

I've also written an article on how to get the type of the array elements from an array type.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev