HarmonyOS 鸿蒙Next This comparison appears to be unintentional because the types

HarmonyOS 鸿蒙Next This comparison appears to be unintentional because the types ‘“1”’ and ‘“2”’ have no overlap
在buildModeSet中设置了不同环境的ENVIRONMENTAL的值,然后在代码中判断 BuildProfile.ENVIRONMENTAL的值:

getBaseUrl(): string {
    Logger.debug(`NetworkUtil => getBaseUrl ENVIRONMENTAL is : ${BuildProfile.ENVIRONMENTAL}`)
    if ('1' == BuildProfile.ENVIRONMENTAL) {
      // 开发环境
      return '1'
    } else if ('2' == BuildProfile.ENVIRONMENTAL) {
      // 测试环境
      return '2'
    } else {
      // 生成环境
      return '3'
    }
  }
复制
"buildModeSet": [
      {
        "name": "debug",
        "buildOption": {
          "arkOptions": {
            "buildProfileFields": {
              "ENVIRONMENTAL": "1"
            }
          }
        }
      },
      {
        "name": "alpha",
        "buildOption": {
          "arkOptions": {
            "buildProfileFields": {
              "ENVIRONMENTAL": "1"
            }
          }
        }
      },
      {
        "name": "beta",
        "buildOption": {
          "arkOptions": {
            "buildProfileFields": {
              "ENVIRONMENTAL": "2"
            }
          }
        }
      },
      {
        "name": "release",
        "buildOption": {
          "arkOptions": {
            "buildProfileFields": {
              "ENVIRONMENTAL": "0"
            }
          }
        }
      }
    ]

编译时报错:This comparison appears to be unintentional because the types ‘“1”’ and ‘“2”’ have no overlap. Product的Build Mode配置的beta


更多关于HarmonyOS 鸿蒙Next This comparison appears to be unintentional because the types的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
BuildProfile.ENVIRONMENTAL明确转为string后再判断,改成:if ('1' == BuildProfile.ENVIRONMENTAL.toString())  或者 let envtype:string = BuildProfile.ENVIRONMENTAL,然后 if('1' == envtype) 进行判断

更多关于HarmonyOS 鸿蒙Next This comparison appears to be unintentional because the types的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next的开发环境中,遇到类型不匹配的错误提示“This comparison appears to be unintentional because the types ‘“1”’ and ‘“2”’ have no overlap”通常意味着你试图比较两个不兼容或完全不同的数据类型。在这个具体的错误信息中,类型“‘1’”和“‘2’”很可能是字符串字面量,但它们的内容或上下文导致了类型不兼容的误解。

这种错误常见于以下几种情况:

  1. 变量或常量被错误地声明或赋值,导致类型不匹配。
  2. 使用了错误的比较操作符或方法。
  3. 编译器或开发环境对类型推断有误。

解决此问题的关键在于:

  • 确认参与比较的两个变量的确切类型,并确保它们是可比较的。
  • 检查赋值和声明语句,确保类型正确无误。
  • 如果使用了泛型或模板,请确保类型参数正确传递。
  • 清理并重建项目,有时IDE或编译器的缓存可能导致类型推断错误。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部