Reputation: 963
If I have a type for a custom array that I get from a node_module (meaning I cant change it)
export type CustomArray = Array<
{
a: string;
b: string;
}
>
what is the proper way to define the type of one of this array`s item?
So far I figured out that that
type CustomArrayItem = CustomArray[0]
or
type CustomArrayItem = CustomArray[number]
works but I am unsure if this is right because I could not find anything about it in the Docs.
Upvotes: 0
Views: 134
Reputation: 249616
As you discovered, both work well enough. I would use CustomArray[number]
since that basically says 'Give me the type if I index with any number' while CustomArray[0]
says 'Give me the type if I index with 0'. The two seem similar, but if CustomArray
is a tuple type, results may actually be different:
export type CustomArray = [string, ...number[]];
type I0 = CustomArray[0] // string
type I1 = CustomArray[1] // number
type INumber = CustomArray[number] // string | number
Upvotes: 1