Reputation: 66
I’m trying to get function c to accept two types. The types are the same except for the optional id
property in Foo. Flow errors on this even if I check for the existence of id
before using it. I tried searching here and in the docs but I couldn’t find anything.
type Foo = {
bar: string,
id?: string,
}
type Bar = {
bar: string,
};
const a = (args: Bar) => {
b(args);
}
const c = (args: Foo) => {
b(args);
}
const b = ({ bar, id }: Bar | Foo) => {
// As soon as you use id here Flow errors out, even though id is optional
// and I check for it’s existence.
if (id) {
console.log(id);
}
}
View a working example on Try Flow
Upvotes: 1
Views: 491
Reputation: 3201
Flow currently (v0.69) has some weaknesses over object destructuring, you can achieve what you want without destructuring:
const b = (obj: Bar | Foo) => {
if (obj.id) {
console.log(obj.id)
}
}
Upvotes: 1