Let's say I have a type like:
type Person = {
firstName: string;
lastName: string;
contact: {
type: string;
value: string;
}[];
};
If I want the type of an element of the contact array, I can use an indexed access type like:
type Contact = User['contact'][number];
// same as type Contact = { type: string; value: string };
In essence, "the type at a numeric index of the contacts array", and would also work for a nested object.
However, if that is an optional parameter such as:
type Person = {
firstName: string;
lastName: string;
contact?: {
type: string;
value: string;
}[];
};
This (validly) reports an error of:
Type '{ type: string; value: string; }[] | undefined' has no matching index signature for type 'number'.ts(2537)
How can I "null check" within the type alias to get a nested type?