【发布时间】:2017-02-18 16:44:34
【问题描述】:
我有这种情况,没有办法有意义地改变数据结构。所以我不能添加标签。 有没有办法区分没有标签的类型?我尝试了鸭式打字,但它不起作用。见我的example
type Result = Done | Error; // a disjoint union type with two cases
type Done = { count: number }
type Error = { message: string }
const doSomethingWithDone = (obj: Done) => {/*...*/}
const doSomethingWithError = (obj: Error) => {/*...*/}
const f = (result: Result) => {
if (result.count) {
doSomethingWithDone(result)
} else {
doSomethingWithError(result)
}
}
错误是:
5: const doSomethingWithDone = (obj: Done) => {/*...*/}
^ property `count`. Property not found in
10: doSomethingWithDone(result)
^ object type
6: const doSomethingWithError = (obj: Error) => {/*...*/}
^ property `message`. Property not found in
12: doSomethingWithError(result)
^ object type
【问题讨论】:
标签: javascript flowtype