I want to create a type NestedKeys that iterates over the given nested type RootNav and collects all keys where the value is Nested<T> and make this a union type of strings containing the keys, while following the nested structure (maybe recursive?)
type Nav = {
[key: string]: NestedNav<Nav> | object | undefined
}
type NestedNav<T extends Nav> = T
type RootNav = {
LoginNav: NestedNav<LoginNav>;
RegistrationNav: NestedNav<RegistrationNav>;
AppNav: NestedNav<AppNav>
}
type AppNav = {
MainNav: NestedNav<MainNav>;
FooScreen: undefined
BarScreen: {id: string}
};
type LoginNav = {
LoginScreen: undefined
}
type RegistrationNav = {
RegistrationScreen: undefined
}
type MainNav = {
HomeScreen: undefined
ProfileScreen: undefined
}
The endresult should be
type NestedKeys<RootNav>
// → "RootNav" | "LoginNav" | "RegistrationNav" | "AppNav" | "MainNav"
I had something in mind like this, but don't know how to do it properly. This doesn't work:
type NestedKeys<T extends Nav> = T[keyof T] extends NestedNav<any> ? NestedKeys<T[keyof T]> : T```
It is possible to do but it requires a small refactor of types. TypeScript does not support
macros. There is no concept ofvalue.toStringlike in javascript. It means that having some type you are unable to get string representation of type name.That's why I have added
tagproperty:Playground
type
Prefixrepresents any nav name.type
Navrepresents valid nav type.type
GetNamesiterates recursively throughnavtype and addstagproperty toCacheif such exists.