OpenHarmony升級4.0后,编译报错。

1、 Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)

报错信息:

ERROR: ArkTS:ERROR File: .../WeatherRow.ets:99:12
 Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)

错误代码:

  ForEach(this.weatherCityList, (item: string) => {
          ...
        }, item => item)

修改代码:

  ForEach(this.weatherCityList, (item: string) => {
          ...
        }, (item:string) => item)

错误代码:

  this.screenModeModel.getDisplayOffTimeValue().then(index=>{
      this.AnimationText=this.checked=this.AnimationMultiplier[index as number]
    })

修改代码:

 this.screenModeModel.getDisplayOffTimeValue().then((index:number)=>{
      this.AnimationText=this.checked=this.AnimationMultiplier[index]
    })

错误代码:

  private AnimationMultiplier;

修改代码:

private AnimationMultiplier: Resource[] = [$r('app.string.seconds', 30), $r('app.string.seconds', 60), $r('app.string.minutes',5), $r('app.string.never')];

错误代码:

   private appItem: any

修改代码:


2、Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals)

报错信息:

ERROR: ArkTS:ERROR File: ...AppData.ets:19:27
 Array literals must contain elements of only inferrable types (arkts-no-noninferrable-arr-literals)

错误代码:

export function appInit(): Array<AppInfo> {
   let appInfo = [
    { bundleName: 'com.example.myapplication', name: 'myapplication', icon: $r('app.media.icon') },
    { bundleName: 'com.example.distributedcalc', name: '计算器', icon: $r('app.media.calculator') }
   ]
  return appInfo
}

修改代码:

export function appInit(): Array<AppInfo> {
  let appInfo = [
    new AppInfo('com.example.myapplication', 'myapplication', $r('app.media.icon')),
    new AppInfo('com.example.distributedcalc', '计算器', $r('app.media.calculator'))
  ]
  return appInfo
}

3、 Argument of type 'string | undefined' is not assignable to parameter of type 'string'.

报错信息:

ERROR: ArkTS:ERROR File: ...Util.ets:100:33
 Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

错误代码:

    /**
     * The bundle name of the application.
     *
     * @syscap SystemCapability.ResourceSchedule.UsageStatistics.App
     * @systemapi Hide this for inner system use.
     * @since 9
     */
    bundleName?: string;
   ...
   let appTime = new AppTime(res[key].bundleName, res[key].abilityPrevAccessTime, res[key].abilityInFgTotalTime)

修改代码:

    let appTime = new AppTime(res[key].bundleName!, res[key].abilityPrevAccessTime!, res[key].abilityInFgTotalTime!)

4、 Type 'AppTime[]' is not assignable to type 'never[]'.

报错信息:

ERROR: ArkTS:ERROR File: ...Util.ets:110:3
 Type 'AppTime[]' is not assignable to type 'never[]'.
  Type 'AppTime' is not assignable to type 'never'.

错误代码:

   let statisticals = []
   let list: Array<AppTime> = []
   statisticals = list

修改代码:

   let statisticals: Array<AppTime> = []
   let list: Array<AppTime> = []
   statisticals = list

5、 Argument of type 'string' is not assignable to parameter of type 'never'.

报错信息:

ERROR: ArkTS:ERROR File: ...Util.ets:113:20
 Argument of type 'string' is not assignable to parameter of type 'never'.

错误代码:

  let panelName = []
  ...
  export default class AppTime {
    bundleName: string //应用的包名
  ...
  panelName.push(getAppName(statisticals[i].bundleName))

修改代码:

   let panelName:Array<string>  = []
    ...
    export default class AppTime {
      bundleName: string //应用的包名
    ...
    panelName.push(getAppName(statisticals[i].bundleName))

6、"for .. in" is not supported (arkts-no-for-in)

报错信息:

ERROR: ArkTS:ERROR File: ...Util.ets:97:3
 "for .. in" is not supported (arkts-no-for-in)

错误代码:

  let res:usageStatistics.BundleStatsMap = await usageStatistics.queryBundleStatsInfos(0, 20000000000000)
  for (let key in res) {
  }

修改代码:

  let bundleName:string
  let abilityPrevAccessTime:number
  let abilityInFgTotalTime:number
  JSON.parse(JSON.stringify(res).toString(),(key:string, value:myBundleStatsInfo) =>{
    if (key==="bundleName") {
      bundleName=value as string
    }
    if (key==="abilityPrevAccessTime") {
      abilityPrevAccessTime=value  as number
    }

    if (key==="abilityInFgTotalTime") {
      abilityInFgTotalTime=value  as number
    }
    if (key===bundleName) {
      console.error('bundleName=' + bundleName+"abilityPrevAccessTime="+abilityPrevAccessTime);
      let appTime = new AppTime(bundleName!,abilityPrevAccessTime!,abilityInFgTotalTime!)
      list.push(appTime)
    }
  });

6、Indexed access is not supported for fields (arkts-no-props-by-index)

报错信息:

ERROR: ArkTS:ERROR File: ...WeatherUtil.ets:61:31
 Indexed access is not supported for fields (arkts-no-props-by-index)

错误代码:

 resultTemp = JSON.stringify(resultObject["data"]["weather"])

修改代码:


7、Use arrow functions instead of function expressions (arkts-no-func-expressions)

报错信息:

ERROR: ArkTS:ERROR File: screenAndBrightness.ets:126:50
 Use arrow functions instead of function expressions (arkts-no-func-expressions)

错误代码:

JSON.parse(JSON.stringify(res).toString(), function (key:string, value:myBundleStatsInfo) {
        console.log('dhf_ key=' + key);
        console.log('dhf_ value=' + value);
      });

修改代码:

   JSON.parse(JSON.stringify(res).toString(),(key:string, value:myBundleStatsInfo) =>{
        console.log('dhf_ key=' + key);
        console.log('dhf_ value=' + value);
      });

8、Property 'extraData' has no initializer and is not definitely assigned in the constructor.

报错信息:

ERROR: ArkTS:ERROR File: HttpRequestOptions.ets:22:3
 Property 'extraData' has no initializer and is not definitely assigned in the constructor.

错误代码:

import Logger from './Logger'

const TAG = '[HttpRequestOptions]'

export class HttpRequestOptions {
  method: string
  extraData: Object
  header: Object
  readTimeout: number
  connectTimeout: number

  constructor() {
    this.method = 'GET'
    this.header = {
      'Content-Type': 'application/json'
    }
    this.readTimeout = 5000
    this.connectTimeout = 5000
  }

  setMethod(method: string) {
    this.method = method
    Logger.info(TAG, `setMethod method is ${this.method}`)
  }

  setExtraData(extraData: Object) {
    this.extraData = extraData
    Logger.info(TAG, `setExtraData extraData is ${JSON.stringify(this.extraData)}`)
  }

  setHeader(header: Object) {
    this.header = header
    Logger.info(TAG, `setHeader header is ${JSON.stringify(this.header)}`)
  }
}

export default new HttpRequestOptions()

修改代码:


9、Type 'null' is not assignable to type 'Object'.

报错信息:

ERROR: ArkTS:ERROR File: HttpRequestOptions.ets:22:3
 Type 'null' is not assignable to type 'Object'.

错误代码:

  extraData: Object =null

修改代码:

 extraData: Object =""

10、 Type 'undefined' is not assignable to type 'HttpResponse'.

报错信息:

  ERROR: ArkTS:ERROR File: WeatherUtil.ets:11:5
  Type 'undefined' is not assignable to type 'HttpResponse'.

错误代码:


 let requestData: http.HttpResponse = undefined

修改代码:

  let requestData: http.HttpResponse

posted on 2024-03-15 17:49  EthanDong  阅读(2541)  评论(0编辑  收藏  举报

导航