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>
这个any类型是不让用了吗?这要怎么办呢?
下面是源码:
import DateUtil from '../../utils/DateUtil'
import { font } from '[@kit](/user/kit).ArkUI'
import DateDialog from '../../dialog/DateDialog'
// 定义 Activity 接口
@Preview
@Component
export default struct HomeContent{
onPageShow(): void {
font.registerFont({
familyName: “aliPuHuiTi”, //字体名
familySrc: $rawfile(‘AlimamaAgileVF-Thin.ttf’) //字体资源
})
}
@StorageProp(‘date’) date:number =DateUtil.beginTimeOfDay(new Date())
@State arr : any[]=[
{
name : ‘跳绳’,
icon: $r(‘app.media.skip’) ,
consume :60,
num:10,
target :10,
pre:‘分钟’,
},
{
name : ‘游泳’,
icon: $r(‘app.media.swim’) ,
consume :400,
num:1,
target :2,
pre:‘小时’,
},
{
name : ‘卧推’,
icon:$r(‘app.media.push’) ,
consume :30,
num:5,
target :10,
pre:‘个’,
},
]
controller:CustomDialogController = new CustomDialogController({
builder:DateDialog({date :new Date(this.date)})
})
build() {
Column(){
Column(){
Text(‘Go Fit’)
.fontFamily(‘aliPuHuiTi’)
.fontSize(25)
.fontWeight(600)
.fontStyle(FontStyle.Italic)
.margin({top:10,bottom:100,left:20})
.fontColor(’#ff9900’)
Row(){
Text(DateUtil.formatDate(this.date))
.fontSize(15)
.fontWeight(500)
Image($r(‘app.media.arrow_down’))
.width(20)
}
.width(‘90%’)
.height(40)
.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({ space: 10 }){
ForEach(this.arr,(item)=>{
ListItem(){
Row(){
Image(item.icon.path)
.width(50)
.height(50)
Text(item.name)
.fontSize(13)
.fontWeight(600)
.opacity(0.8)
Blank()
if (item.num===item.target){
Text(‘消耗’+item.consume*item.num+‘卡路里’)
.fontSize(13)
.fontWeight(600)
.margin({right:10})
.fontColor($r(‘app.color.task_color’))
}else {
Text(item.num+’:’+item.target+’/’+item.pre)
.fontSize(13)
.fontWeight(600)
.margin({right:10})
}
}
.width(‘100%’)
.backgroundColor(Color.White)
.borderRadius(15)
}
.width(‘90%’)
})
}
.width(‘100%’)
.alignListItem(ListItemAlign.Center)
}
}
}
.width(‘100%’)
.height(‘100%’)
}
.width(‘100%’)
.height((‘100%’))
}
}
更多关于HarmonyOS 鸿蒙Next:Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>的实战教程也可以访问 https://www.itying.com/category-93-b0.html
鼠标放在arr上会生成对应的文件定义,复制出来定义接口类型
更多关于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
这一lint规则,其核心目的是提升代码的类型安全性和可维护性。该规则鼓励开发者在TypeScript或类似支持类型检查的语言环境中,尽量避免使用any
和unknown
类型,因为它们会绕过类型检查系统,可能导致运行时错误。
要解决这个问题,你需要:
-
明确类型定义:对于原本使用
any
或unknown
的变量或函数参数,尽可能明确其具体的类型。例如,如果某个变量实际上是字符串数组,应将其类型定义为string[]
而非any[]
或unknown[]
。 -
使用类型断言:在确实无法直接确定类型但确信类型安全的情况下,可以使用类型断言来明确类型,但这应作为最后手段,避免滥用。
-
类型守卫:对于可能是多种类型的变量,可以使用类型守卫(type guards)来在运行时检查类型,并据此处理不同的类型情况。
通过上述方法,你可以有效减少或消除代码中的any
和unknown
使用,提高代码的健壮性和可维护性。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html