// 1 - custom union of two values belonging to different types
interface OperationResult {
success?: true // type is true | undefined
}
// 2 - native union, type boolean
interface OperationResult {
success: boolean // type is boolean = true | false
}
第二种更准确,因为它使用语言中现有的主要联合类型,因此我们使用最简单的可能选项 - boolean,它是具有两个可能值的类型。第一个选项还使用具有两个可能值的类型,但这是自定义联合。两种方法都是同构的(我们可以用另一种方法替换),因为显然关系是 -false表示为undefined and true表示为true。第一个选项并不优于第二个选项,它是相等的,但是引入第二个二进制类型而不是布尔值,这确实是不想要的,我的意思是没有理由这样做。