Categories
TypeScript Answers

How to get type of array items with TypeScript?

Spread the love

Sometimes, we want to get type of array items with TypeScript.

In this article, we’ll look at how to get type of array items with TypeScript.

How to get type of array items with TypeScript?

To get type of array items with TypeScript, we can use lookup types.

For instance, we write

type Foo = Array<{ name: string; test: number }>;
type FooItem = Foo[0];

to get the type for array entries in the Foo type.

To do this, we just use Foo[0] to return the type of the items in with the Foo array type.

And we assign the returned type to FooItem.

Conclusion

To get type of array items with TypeScript, we can use lookup types.

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 *