HarmonyOS 鸿蒙Next:咨询一下Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTS

发布于 1周前 作者 songsunli 来自 鸿蒙OS

HarmonyOS 鸿蒙Next:咨询一下Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTS
编译阶段报错 Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck>

代码如下:

window: ArkTSFunModel = {
    setTokenInfo: (tokenJson: string) => this.setTokenInfo(tokenJson),
    routerPush: (page: string) => this.routerPush(page),
    routerPushWeb: (url: string) => this.routerPushWeb(url),
    __dcloud_weex_postMessage: (json: string) => this.postMessage(json)
    // __dcloud_weex_postMessage: (json: string) => this.postMessage(json),
};

export interface ArkTSFunModel {
    __dcloud_weex_postMessage: (json: string) => void;
    setTokenInfo: (tokenJson: string) => void;
    routerPush: (page: string) => void;
    routerPushWeb: (url: string) => void;
}

不知道为何会有如此的报错.


更多关于HarmonyOS 鸿蒙Next:咨询一下Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTS的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复
let window: ArkTSFunModel = {
  setTokenInfo: (tokenJson: string): void => setTokenInfo(tokenJson),
  routerPush: (page: string): void => routerPush(page),
  routerPushWeb: (url: string): void => routerPushWeb(url),
  __dcloud_weex_postMessage: (json: string): void => postMessage(json)
};

function setTokenInfo(json:string){
  return
}

function routerPush(json:string){
  return
}

function routerPushWeb(json:string){
  return
}

function postMessage(json:string){
  return
}

export interface ArkTSFunModel {
  __dcloud_weex_postMessage: (json: string) => void;
  setTokenInfo: (tokenJson: string) => void;
  routerPush: (page: string) => void;
  routerPushWeb: (url: string) => void;
}

let PAGES_DICT = new Map<string, string>([['/pages/setting/setting', 'pages/Setting'], ['/pages/midPage/midPage', 'pages/Home']]);
@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
}

可以参考https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/arkts-more-cases.md#arkts-no-untyped-obj-literals,里面介绍了一些规范。

更多关于HarmonyOS 鸿蒙Next:咨询一下Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTS的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next系统中,当你遇到“Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals)”这个错误时,这通常意味着你在ArkTS(Ark TypeScript)代码中创建了一个对象字面量,但没有将其与任何明确声明的类或接口相关联。

ArkTS是鸿蒙系统用于开发UI组件的一种编程语言,它基于TypeScript扩展而来,增加了对UI组件特性的支持。在ArkTS中,所有的UI组件都需要基于类进行定义,对象字面量通常不被允许直接用作组件或数据结构的定义,除非它们明确地符合某个类或接口的结构。

解决这个问题的方法是:

  1. 确保你创建的对象字面量符合某个已声明的类或接口的结构。
  2. 如果你是想要创建一个组件实例,应该使用类定义并通过new关键字来实例化,而不是使用对象字面量。
  3. 检查你的ArkTS代码,找到触发这个错误的对象字面量,并根据需要将其转换为类实例或调整其结构以符合某个类或接口。

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

回到顶部