Is there a way to convert a string URL into an object type in TypeScript?
Here is some sample code:
type KeyUrl<T> = T extends `/${infer U}` ? U : never;
type TUrl<T> = { [k in KeyUrl<T>]: string };
// --------------------------------------------- This line works
const url = "/path";
const obj = {} as TUrl<typeof url>;
obj.path // It works;
// --------------------------------------------- This line throws an error
const url = "/path/path2";
const obj = {} as TUrl<typeof url>;
obj.path // Property 'path' does not exist;
obj.path2 // Property 'path2' does not exist;
Thank you.