I am using react-router v6 in my application, and I need all paths in the router config object to be absolute
It is crucial for the path of a child route at any depth to start with its parent path to avoid potential crashes
It appears that a recursive type with template literals may be necessary
The type I have devised (with only path and children for simplicity) is as follows:
type Route<T extends string> = {
path: T;
children?: Route<`${T}/${string}`>[];
};
However, this approach does not work for nested routes, where the generic T
reverts to a string
type
const invalidRoute: Route<"/home"> = {
path: "/home",
children: [
{
path: "/home/about",
children: [
{
// Expected type `/home/${string}/${string}` here, got `/home/about/${string}`
// This lack of TypeScript error makes it challenging
path: "/home/nonabout/whatever",
},
],
},
],
}
Any assistance would be greatly appreciated!