Categories
TypeScript Answers

How to create an empty typed container array with TypeScript?

Spread the love

Sometimes, we want to create an empty typed container array with TypeScript.

In this article, we’ll look at how to create an empty typed container array with TypeScript.

How to create an empty typed container array with TypeScript?

To create an empty typed container array with TypeScript, we can create the variable and set it to the array type or we can cast an existing to the array type.

For instance, we write

const arr: Fruit[] = [];

to create the arr array to a Fruit array type.

We can also cast an array to the type we want by writing

const arr = <Fruit[]>[];

Conclusion

To create an empty typed container array with TypeScript, we can create the variable and set it to the array type or we can cast an existing to the array type.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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