Reactjs 如何修复动态布尔变量赋值的FlowJS错误?

Reactjs 如何修复动态布尔变量赋值的FlowJS错误?,reactjs,flowtype,Reactjs,Flowtype,Flow抛出一个错误,称为anotherEmail:string(此类型与boolean不兼容) 如何修复此问题?您看到的错误发生的原因是 //Person Type type Person{ canDisplayButton:boolean, anotherEmail:string } const canEdit:boolean = person.canDisplayButton && data.anotherEmail; 正如您定义的那样,canEdi

Flow抛出一个错误,称为
anotherEmail:string(此类型与boolean不兼容)


如何修复此问题?

您看到的错误发生的原因是

//Person Type  
type Person{
    canDisplayButton:boolean,
    anotherEmail:string
}

const canEdit:boolean = person.canDisplayButton && data.anotherEmail;
正如您定义的那样,
canEdit
是布尔值-为其指定字符串-将导致错误。因此,解决方案需要为其分配适当的类型

const canEdit = true && 'text';

// results into

canEdit // 'text'

如果您试图确定是否定义了
data.anotherEmail
,您不能使用
!!数据。另一封邮件
来确定变量的真实性?

也可以是未定义的,对吗?我还有更多的领域需要评估。我正在查看是否有
解决方案?如果您试图确定是否定义了
数据。是否定义了另一封邮件
,您不能使用
!!数据。是否发送电子邮件来确定变量的真实性?是,
工作。使用
Boolean()
也有效。在这种情况下,我已将其更改为一个答案-请您将其标记为正确@titogeo?谢谢titogeo,非常感谢!或者以更明确的方式,
Boolean(data.anotherEmail)
const canEdit:boolean = person.canDisplayButton && data.anotherEmail !== '';

// or 

const canEdit:boolean = Boolean(person.canDisplayButton && data.anotherEmail);

// or

let canEdit = false;

if (person.canDisplayButton && data.anotherEmail) {
    canEdit = true;
}