typescript - 打字稿中包含联合类型的对象的可区分联合
问题描述
请建议一个更好的标题,我不确定到底要问什么。
我有一个看起来像这样的类型定义:
type Animal =
| {
'species': 'cat'
'action': 'meow'
}
| {
'species': 'cat'
'action': 'whine'
}
| {
'species': 'dog' | 'tree'
'action': 'bark'
};
我想定义一个条件类型ActionsFor<S>
,导致给定物种的缩小类型。例如:
type CatActions = ActionsFor<'cat'> // should be 'meow' | 'whine'
type DogActions = ActionsFor<'dog'> // should be 'bark'
type TreeActions = ActionsFor<'tree'> // should be 'bark'
type BarkActions = ActionsFor<'dog'|'tree'> // should be 'bark'
我目前的尝试很接近,但不能如我所愿地与联合物种一起工作:
type ActionFor<S extends Animal['species']> = Extract<Animal, {species: S}>['action']
结果是:
type CatActions = ActionsFor<'cat'> // correct - 'meow' | 'whine'
type DogActions = ActionsFor<'dog'> // WRONG - never
type TreeActions = ActionsFor<'tree'> // WRONG - never
type BarkActions = ActionsFor<'dog'|'tree'> // correct - 'bark'
如何重新定义 ActionsFor 来做我想做的事?
解决方案
弄清楚了。这似乎可行,但如果有人可以让它更短或更优雅,我会全力以赴!
type ActionFor<S extends Animal['species']> = Exclude<Animal, {species: Exclude<Animal['species'], S>}>['action']
推荐阅读
- rust - 如何在 Rust 中测试类型相等性?
- mysql - 完全访问用户的数据库权限错误
- python-3.x - AttributeError:硒上的“NoneType”
- java - 为什么 Quartz Scheduler 在 x 分钟后停止在 Heroku 上工作?
- jquery - Zendesk Ticket 使用 ajax 创建 API
- php - PDFLib 已安装但在代码中不可用或通过 phpinfo() 可见
- android - Android BluetoothHeadset getConnectedDevices() 列表为空
- php - 从另一个相关表中的计数仅为一个的表中选择
- java - 哪个先中断,Mockito 还是 Java 泛型?
- linux - 使用读/写文件的管道()的替代实现