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.