Below is the code snippet:
interface MyInterface {
a: {
b: {
c: "c";
};
};
}
type ParentProps = keyof MyInterface
type ChildProps<ParentProp extends ParentProps> = keyof MyInterface[ParentProp]
type GrandChildType<ParentProp extends ParentProps, ChildProp extends ChildProps<ParentProp>> =
MyInterface[ParentProp][ChildProp]['c']
An error occurs in the GrandChildType
section:
The type '"c"' cannot be used to index type 'MyInterface[ParentProp][ChildProp]'.(2536)
However, when the last line is altered to:
type GrandChildType<ParentProp extends ParentProps, ChildProp extends ChildProps<ParentProp>> =
MyInterface[ParentProp][ChildProp]
type test = GrandChildType<'a', 'b'>['c']
The correct type of c
, which is "c"
, is obtained. So, the question arises: why is it not possible to retrieve that value type using Generics if both ParentProps
and ChildProps
are keys extracted from MyInterface
?
Reproducible link: typescript playground