我有过
type Route = {
path: string
children?: readonly Route[]
}
const root = {
path: "a",
children: [
{ path: "b" },
{ path: "c", children: [{ path: "d", children: [{ path: "e" }] }] }
],
} as const satisfies Route;
我想定义一个类型脚本泛型Convert
,这样Convert<typeof root>
的输出就是字符串枚举‘a’|‘a/b’|‘a/c’|‘a/c/d’|‘a/c/d/e’
type Convert<T extends Route> = ?
我试过像这样的东西
type Convert<T extends Route, Prefix extends string = ''> =
`${Prefix}/${T['path']}` | (
T["children"] extends readonly Route[]
? {
[K in number]: Convert<T['children'][K], `${Prefix}/${T['path']}`>;
}[number]
: never)
但是输出="/a"|"/a/b"|"/a/c"还不够深入.
感谢您的帮助!