Let's say I have
interface Animal {}
interface Group<A extends Animal> {}
And I want to create a generic interface over Group
interface AnimalGroupProps<G extends Group<A>, A extends Animal> {
withGroup: () => G
// We want to be able to reference A in the interface, for use like this:
withLeader: () => A
}
I wish for animal group props to be generic over the group type without explicitly declaring A as extending Animal:
interface AnimalGroupProps<G extends Group<A>>
If only TypeScript could infer it. However, TypeScript requires A to be declared, forcing me to stick with the previous snippet's pattern.
class Wolf implements Animal {}
class WolfPack implements Group<Wolf> {}
function AnimalPlanetWolves ({withGroup, withLeader}: AnimalGroupProps<WolfPack, Wolf>) {}
// This is the really annoying part --------------------^^^^
All users of AnimalGroupProps are forced to specify both generic parameters even though one of them is redundant, leading to unnecessary repetition in my codebase.
In the example provided, WolfPack was not a generic type. But what if there was a generic type that needed to be passed to AnimalGroupProps? It becomes even more complicated:
interface Flock<A extends Animal> extends Group<A> {}
class Geese implements Animal {}
function AnimalPlanetBirds ({withGroup, withLeader}: AnimalGroupProps<Flock<Geese>, Geese>) {}