【发布时间】:2023-02-06 17:05:17
【问题描述】:
如何将字符串 url 转换为 ts 中的对象类型?
示例代码:
type KeyUrl<T> = T extends `/${infer U}` ? U : never;
type TUrl<T> = { [k in KeyUrl<T>]: string };
// --------------------------------------------- this line work
const url = "/path";
const obj = {} as TUrl<typeof url>;
obj.path // work;
// --------------------------------------------- this line 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;
谢谢。
【问题讨论】:
标签: typescript