Below is a simple generic type, "GetA", that is just supposed to index a type object to retrieve a nested type; it uses generic parameters as two of the indexes. At the last, non-generic index, however, it throws a type error ("a" cannot be used to index...), even though all of the possible objects have the indexed key. I hope that someone can help me understand.
How can I make a working version of GetA that uses the same or similar parameters?
type GetA<
T extends keyof TestObj,
P extends keyof TestObj[T]
> = TestObj[T][P]["a"];
// Type '"a"' cannot be used to index type 'TestObj[T][P]'.ts(2536)
type TestObj = {
nested1: {
prop: {
a: "foo1";
b: "bar1";
};
anotherProp: {
a: "foo2";
b: "bar2"
}
};
nested2: {
prop: {
a: "foo3";
b: "bar3";
};
};
};