, , . . ,
const x: Error = {message: 'foo', count: 'bar'};
f(x);
, x. , , - Error, message, , . , count , Done.
:
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 if (result.message) {
doSomethingWithError(result)
}
}
// Expected error. Since Error is an exact type, the count property is not allowed
const x: Error = {message: 'foo', count: 'bar'};
f(x);
( tryflow)
, , , else else if. , , , . , , , , .