reactjs - 如何修复动态布尔变量分配的 FlowJS 错误?
问题描述
//Person Type
type Person{
canDisplayButton:boolean,
anotherEmail:string
}
const canEdit:boolean = person.canDisplayButton && data.anotherEmail;
Flow 抛出一个错误说anotherEmail: string (This type is incompatible with boolean)
如何解决这个问题?
解决方案
您看到的错误正在发生,因为
const canEdit = true && 'text';
// results into
canEdit // 'text'
正如您定义的那样,它canEdit
是布尔值 - 为其分配一个字符串 - 会导致错误。所以解决方案是为其分配适当的类型
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;
}
推荐阅读
- sql-server - 从 SSIS 脚本任务传递文件名
- matplotlib - 通过 Anaconda 安装的 Windows 中 Cartopy 的存储库在哪里?
- java - Spring Boot tomcat-embed-core-9.0.21.jar:zip 文件为空
- sql - 如何在 SQL Server 中的 DATETIME 列上查询 DATE?
- fiware - 从 FIWARE ORION 中的 OpenWeatherMap 检索预报数据
- python - TypeError: TimestampType 不能接受对象
和 - mysql - 将字符串格式化为日期 MySQL
- javascript - 将数组转换为具有相似键/值对的对象
- excel - 运行时错误“91”:对象变量或未设置块变量 - 工作一次,然后将不再工作
- sql - 如何在 PostgreSQL 中互相减去小时和分钟