typescript - 打字稿守卫在联合类型上失败
问题描述
customer.id
我不明白为什么 TypeScript在这个例子中抱怨一个可能的未定义对象( ):
type Customer = {
id: string
company: string
}
type Props =
| {
mode: 'new'
customer: undefined
}
| {
mode: 'edit'
customer: Customer
}
function form({ mode, customer }: Props) {
const formId = mode === 'edit' ? customer.id : 'foo'
}
据我了解,客户永远不会undefined
像mode
现在这样edit
。我以为这是某种 TypeScript Guard?
解决方案
推荐阅读
- dart - 带有自定义声音的 Firebase 推送通知 (Flutter)
- elasticsearch - 精确匹配和模糊......什么是好方法?
- mysql - SQL - 创建新字段时组中的最大值
- ios - 创建 Zendesk Ticket iOS v2
- bazel - 在 bazel 宏中创建模板文件的最佳方法
- javascript - 在对象中复制和过滤空值时遇到问题
- javascript - 在 React 或 Java 中处理大量记录的正确方法
- gradle - 在 DefaultDependencyHandler 类型的对象上找不到参数 [org.hibernate:hibernate:5.3.7.FINAL] 的方法 intTestImplementation()
- reactjs - 反应:将父母传递给孩子
- python - ValueError:无效的 RGBA 参数:是什么导致了这个错误?