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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.