【发布时间】:2021-05-07 23:00:57
【问题描述】:
这里我们有这个方法:
export const showGiftedOrGiftingEmail = (
transaction: Transaction
): { [a: string]: string } | null => {
它清楚地返回一个可选的,要么是一个对象,要么是null。但是这里它接受没有?操作符的表达式,为什么?
showGiftedOrGiftingEmail(props.transaction).email
或?
showGiftedOrGiftingEmail(props.transaction)?.email
如果 Typescript 不强制实现这种类型安全,那么使用 Typescript 的意义何在? Swift 永远不会接受第一个表达式。
【问题讨论】:
-
您是否开启了严格的空值检查?毕竟,严格的空检查是可选的。
-
"为什么 web 有 null 和 undefined 表达式?" What is the difference between null and undefined in JavaScript?
-
Swift 是如何参与到这段代码中的?
-
对于我删除的第二个问题,请参见例如stackoverflow.com/q/61036776/3001761
-
@VLAZ Swift 参与其中,因为 Swift 也实现了可选模式,但以更合乎逻辑的方式。它们只有一种状态,代表不存在的价值。 JS null / undefined 状态是可以忍受的吗?我只想指出 Swift 解决方案更强大且更易于理解。
标签: swift typescript optional