angular - 将角度路由映射到键是路径属性的类型
问题描述
我有这样的路线
const routes = [
{ path: 'path1' },
{ path: 'path2', children: [{ path: 'path3' }] },
] as const;
我想要一张这样的地图
type map = { path1: {}; path2: { path3: {} } };
那么,如何更正下面的类型以便从上面提供的路线中获取地图?
type MappedRoutes<TRoutes> = {
[K in keyof TRoutes]: TRoutes[K] extends {children: unknown, path: string, }
? {TRoutes[K]['path']: MappedRoutes<TRoutes[K]['children']>}
: TRoutes[K]['path'];
}
也就是说MappedRoutes<ArrayIndexes<typeof routes>>
,如下应该返回{path1: {}; path2: { path3: {} } }
;
const array = [] as const;
type keyOfArray = keyof typeof array;
type ArrayIndexes<TArray> = Exclude<keyof TArray, keyOfArray>;
type mapped= MappedRoutes<ArrayIndexes<typeof routes>>;
解决方案
我可能会这样写:
type Routes = readonly { path: string, children?: Routes }[];
type MappedRoutes<T extends Routes> = {
[K in T[number]["path"]]: (Extract<T[number], { path: K }>['children'] extends infer R ?
R extends Routes ? MappedRoutes<R> extends infer O ? {
[P in keyof O]: O[P]
} : never : {} : never)
}
您可以验证它是否以您想要的方式工作(尽管没有干预ArrayIndices
):
type Mapped = MappedRoutes<typeof routes>
/* type Mapped = {
path1: {};
path2: {
path3: {};
};
} */
如果T
可分配给Routes
, 的数组{path: string, children?: Routes}
, 那么MappedRoutes<T>
键在T[number]["path"]
... 其中T[number]
是数组元素类型的并T[number]["path"]
集,并且是它们path
属性的并集。
然后,对于这些键中的每一个K
,我们得到Extract<T[number], {path: K}>['children']
,这是children
对应于 的属性path
。( Extract<T[number], {path: K}>
) 采用数组元素的并集并仅提取可分配给 的元素{path: K}
。然后我们得到它的children
属性)。
我们检查这个children
属性。是它Routes
本身吗?如果是这样,递归地产生MappedRoutes
它。那里发生了相当多的条件类型推断,但这主要是因为我可以将类型存储到类型变量中而不是重写它们。 R
是children
属性,并且O
是映射的children
属性(如果存在)。我遍历,O
以便该Mapped
类型急切地递归扩展为其最终形式,而不是像{path1: {}, path2: MappedRoutes<readonly blah blah yuck>}
.
好的,希望有帮助;祝你好运!
推荐阅读
- c - 在 Pyinstaller 中使用 Cython
- hash - 我们可以反复对输入进行哈希处理并无限期地对其进行哈希处理吗?
- automation - Vividus 工具无法解析日期表达式
- javascript - 将文本设置为默认值 - V̶u̶l̶n̶e̶r̶a̶b̶i̶l̶i̶t̶y̶ -> 漏洞
- llvm - malloc 或 _Znwm,我们应该在 LLVM IR 系统中使用它
- docusignapi - 如何使用 REST API 定义信封中的文档顺序?
- java - 计算元音的数量并以具有更多元音的字符串优先的方式打印它们
- docker - 在测试网络中使用 Hyperledger Fabric 示例运行“network.sh up”时出错
- numpy - 将矩阵转换为 LAPACK 的最快实现
- django - 如何只保存已更改的模型对象?