typescript - typescript 的可区分联合可以与条件类型一起使用吗?
问题描述
假设我有以下 3 种类型:
type Apple = {
type: "apple"
}
type Banana = {
type: "banana"
}
type Fruit<T extends "apple" | "banana"> = T extends "apple" ? Apple : Banana
我有一个通用功能如下:
function fruit<T extends "apple" | "banana">(fr: Fruit<T>) {
if (fr.type === "apple") {
const element = fr
}
}
为什么element
仍然是 aFruit<T>
而不是Apple
?有没有办法使用条件类型获得有区别的联合工作?
解决方案
推荐阅读
- docker - docker fluentd 这个命令行是什么意思?
- javascript - 如何应用动画关于文本更改
- css - 将 ul 元素添加到 Kendo Grid Column 时,ul 元素始终位于其他列之后
- python - 使用 selenium 和 python 登录后我无法导航到 Instagram 页面
- javascript - 如何强制vue轮播每页显示一个
- python - 从 git 安装的应用程序未找到 Django+Celery 模块错误
- python - 用户点击图表,图表根据 te 事件的 ydata 变化
- javascript - 节点服务器本地主机问题
- java - Java - 通过输入记录动态生成 XML 数据
- nginx - 如何使用 nginx 公开此目录?<