HarmonyOS鸿蒙Next中Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>报错无法处理

HarmonyOS鸿蒙Next中Use explicit types instead of “any”, “unknown” (arkts-no-any-unknown) <ArkTSCheck>报错无法处理

跟着不少24年的项目练手的时候都会出现该报错

Use explicit types instead of “any”, “unknown” (arkts-no-any-unknown) <ArkTSCheck>

不知道如何更改,

如下代码所示

import DateDialog from "../../dialog/DateDialog"
import DateUtil from "../../utils/DateUtil"
@Preview
@Component

export default struct HomeContent{
  @StorageProp('date')
  //@Watch('aboutToAppear')
  date: number = DateUtil.beginTimeOfDay(new Date())//参数传入new Date

  @State arr=[

    {
      name: '跳绳',
      icon: $r('app.media.skip'),
      consume: 60,
      num:10,
      target:10,
      pre:'分钟'
    },
   {
   name: '卧推',
   icon: $r('app.media.push'),
   consume: 30,
   num:50,
   target:10,
   pre:'个'
   },
    {
    name: '游泳',
    icon: $r('app.media.swim'),
    consume: 400,
    num:1,
    target:2,
    pre:'小时'
  }
  ]
  controller: CustomDialogController = new CustomDialogController({
    builder: DateDialog({ date: new Date(this.date)})
  })
  build() {
    Column(){
      Column(){
        Text('百战健身')
          .fontSize(25)
          .fontWeight(600)
          .margin({ top: 10, bottom: 100, left: 20})
        Row() {
          Text(DateUtil.formatDate(this.date))
            .fontSize(15)
            .fontWeight(500)
          Image($r('app.media.arrow_down'))
            .width(20)

        }
        .width('90%')
        .height(50)
        .backgroundColor(Color.White)
        .margin({ left: 19, top: 90})
        .borderRadius(20)
        .justifyContent(FlexAlign.Center)//居中展示
        .onClick(() => {
          this.controller.open()
        })
      }
      .backgroundImage($r('app.media.home_bg'))
      .backgroundImageSize({ width: '100%', height: '100%'})
      .width('100%')
      .height('40%')
      .alignItems(HorizontalAlign.Start)
      .borderRadius({ bottomLeft: 20, bottomRight: 20})

      Stack(){
        Column(){
          Text('任务列表')
            .fontSize(13)
            .fontWeight(700)
            .margin({ left: 20, top: 20, bottom: 10})
          Column(){

            List(){
              ForEach(this.arr,(item)=>{
                ListItem(){
                  Row(){
                    Image(item.icon)
                      .width(50)
                      .height(50)
                    Text(item.name)
                      .fontSize(13)
                      .fontWeight(600)
                      .opacity(0.8)
                    Blank()
                    if(item.amount === item.successAmount) {
                      Text('消耗' + item.calorie + '卡路里')
                        .fontSize(13)
                        .fontWeight(600)
                        .margin({ right: 10})
                        .fontColor($r('app.color.task_color'))
                    } else {
                      Text(item.successAmount + ':' + item.amount + item.recordItem.unit)
                        .fontSize(13)
                        .fontWeight(600)
                        .margin({right: 10})
                    }
                  }
                  .width('100%')
                  .backgroundColor(Color.White)
                  .borderRadius(15)
                }
                .width('90%')
              })
            }
            .width('100%')
            .alignListItem(ListItemAlign.Center)
          }
          .width('100%')
        }
      }
      .width('100%')
      .height('100%')
    }
    .width('100%')
    .height('100%')
  }

其中定义的arr:any会报错,导致后面的item也报错,求解答


更多关于HarmonyOS鸿蒙Next中Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>报错无法处理的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

用interface

更多关于HarmonyOS鸿蒙Next中Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>报错无法处理的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,arkts-no-any-unknown规则要求避免使用anyunknown类型。出现该报错时,需将变量声明为具体类型。例如:

错误写法:

let data: any = fetchData();

正确写法:

let data: string = fetchData();

TypeScript会强制类型检查,确保类型安全。如无法确定具体类型,可使用联合类型或泛型替代any/unknown。该规则适用于ArkTS代码检查。

这个错误是ArkTS的类型检查规则导致的,要求开发者避免使用any/unknown这种宽泛类型。针对你的代码,解决方案是:

  1. 首先为数组元素定义明确的接口类型:
interface SportItem {
  name: string;
  icon: Resource;
  consume: number;
  num: number;
  target: number;
  pre: string;
}
  1. 然后修改数组声明:
@State arr: SportItem[] = [
  {
    name: '跳绳',
    icon: $r('app.media.skip'),
    consume: 60,
    num:10,
    target:10,
    pre:'分钟'
  },
  // 其他项...
]
  1. 这样后续使用item时也会自动获得类型推断,比如item.name会被识别为string类型。

这种类型定义方式既能通过ArkTS检查,又能获得更好的代码提示和类型安全。如果数组中某些字段可能不存在,可以用可选属性标记:field?: type

回到顶部