HarmonyOS 如何自己定义一种结构

Record<"title", string>
  • 1.

想将这个结构定义成ClassA,其他地方可以直接使用ClassA。

HarmonyOS
2025-01-09 15:13:43
684浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

参考示例如下:

interface RecordType {
  age: number;
  name: string;
}

type RecordName = 'title';
let recordType: RecordType = { name: 'ZhangSan', age: 5 };
let dataInfo: Record<RecordName, RecordType> = {
  'title': recordType,
};
console.log('打印dataInfo:', JSON.stringify(dataInfo));
console.log("打印结果1:" + JSON.stringify(dataInfo['title']))
console.log("打印结果2:" + JSON.stringify(dataInfo['xxx']));
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

也可以参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-arkts-98-V5

分享
微博
QQ
微信
回复
2025-01-09 17:18:47


相关问题
使用Promise实现一种串行调用方式
1871浏览 • 1回复 待解决
HarmonyOS一种类似总线通知的功能
1047浏览 • 1回复 待解决