Categories
TypeScript Answers

How to use typed arrays with TypeScript?

Spread the love

Sometimes, we want to use typed arrays with TypeScript.

In this article, we’ll look at how to use typed arrays with TypeScript.

How to use typed arrays with TypeScript?

To use typed arrays with TypeScript, we can use the Array type.

For instance, we write

const possessions: Array<Thing> = new Array<Thing>();

to create the possessions variable of type Array<Thing>, which means all entries in the possessions array must be of type Thing.

We can also shorten it to

const possessions: Thing[] = [];

Conclusion

To use typed arrays with TypeScript, we can use 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 *