typescript - 我可以在 Typescript 中提取类型的嵌套结构以使用它来声明新类型吗?
问题描述
考虑如下定义的类型:
type primaryType = {
level1: {
level12: {
a: string
b: string
c: string
}
}
...
}
现在我想创建一个新类型,它是从带有 key 的属性中推断出来的level12
,因此,它将隐含地具有属性a
,b
并且c
:
// Something like this (notation is not valid, used just to visualize the idea):
type inferredType = primaryType.level1.level12
// Not this:
type inferredType = {
a: string
b: string
c: string
}
我扫描了实用程序类型,但无法为此目的进行转换。
有没有办法做到这一点?
解决方案
那么你可以这样做:
type inferredType = primaryType['level1']['level12']
推荐阅读
- reactjs - 用户使用reactjs提交数据时如何只播放声音
- javascript - Javascript 对象实例在 Array Recall 上失去函数访问权限
- html - 如何在 flexbox 的子项之间设置自定义距离
- android - AndroidStudio 无法同步 Gradle 文件
- javascript - CSOM 无法通过 SP 未定义
- javascript - 使用谷歌电子表格作为 tumblr 主题的数据库?
- c# - Asp.net Core DI:使用 SemaphoreSlim 通过 Singleton 进行读写操作
- report - 如何在 BIRT 报表设计器中设置行距
- php - 使用 Laravel 对 JSON 字段进行不区分大小写的查询
- windows - 为什么 cygwin 找不到 Windows OpenSSH 目录?