I am extending existing interfaces with additional properties, which works fine
interface BaseAnimal {
name: string,
age: number
}
interface Dog extends BaseAnimal {
breed: string,
alive: boolean
}
interface Cat extends BaseAnimal {
origin: string,
height: number,
furious: boolean
}
type Animal = (Dog | Cat)
type Animals = (Dog | Cat)[]
type WithAdditionalProperties<T> = {
properties: T;
city: string | null;
country: string
};
export type AdditionalAnimalProps = WithAdditionalProperties<Animals[number]>;
export type AdditionalAnimalsProps = WithAdditionalProperties<Animals[number]>[];
However, when I change the type of
type Animals = (Dog | Cat)[]
to
type Animals = (Dog | Cat)[] | undefined
this
export type AdditionalAnimalProps = WithAdditionalProperties<Animals[number]>;
export type AdditionalAnimalsProps = WithAdditionalProperties<Animals[number]>[];
is not working anymore, which is expected.
Type Animals has no matching index signature for type number
How do I get around this? Here is a Playground