HarmonyOS5ArkTS常见数据类型认识

在 ArkTS(基于 TypeScript 的 HarmonyOS 开发语言)中,常见数据类型主要分为基础类型和复杂类型两大类,以下是核心整理:


⚙️ ​​一、基础类型(Primitive Types)​​

  1. ​​number​​

    • 数值类型,包含整数和浮点数(​无单独的 int/float 区分​)
    ini 复制代码
    typescript
    复制
    let age: number = 30;       // 整数
    let price: number = 9.99;   // 浮点数
  2. ​​string​​

    • 文本类型,支持单引号/双引号/模板字符串
    ini 复制代码
    typescript
    复制
    let name: string = "Alice";
    let msg: string = `Hello ${name}`;  // 模板字符串
  3. ​​boolean​​

    • 布尔值,仅 true 或 false
    ini 复制代码
    typescript
    复制
    let isActive: boolean = true;
  4. ​​void​​

    • 函数无返回值
    javascript 复制代码
    typescript
    复制
    function logError(): void {
      console.error("Error!");
    }
  5. ​​null 和 undefined​​

    • null:显式空值
    • undefined:变量未初始化默认值
    csharp 复制代码
    typescript
    复制
    let data: string | null = null;  // 联合类型允许赋值为 null

🧩 ​​二、复杂类型(Complex Types)​​

​​集合类型​​

  1. ​​数组 Array<T>​​

    • 相同类型元素集合
    typescript 复制代码
    typescript
    复制
    let numbers: number[] = [1, 2, 3];
    let strings: Array<string> = ["a", "b"]; // 泛型语法
  2. ​​元组 Tuple​​

    • ​固定长度​ 和​类型顺序​的数组
    typescript 复制代码
    typescript
    复制
    let userInfo: [string, number] = ["Alice", 25];
    let firstElement: string = userInfo[0]; // 类型安全访问
  3. ​​枚举 enum​​

    • 命名常量集合(自动赋值或手动指定)
    ini 复制代码
    typescript
    复制
    enum Color { Red = 1, Green, Blue }; // Green=2, Blue=3
    let bg: Color = Color.Green;

​​对象类型​​

  1. ​​接口 Interface​​

    • 定义对象结构(​最常用​)
    ini 复制代码
    typescript
    复制
    interface User {
      id: number;
      name: string;
      isAdmin?: boolean;  // 可选属性
    }
    
    let user: User = { id: 1, name: "Bob" };
  2. ​​类 Class​​

    • 面向对象编程(封装/继承/多态)
    typescript 复制代码
    typescript
    复制
    class Person {
      constructor(public name: string) {}
      greet() { console.log(`Hello ${this.name}`) }
    }

​​特殊类型​​

  1. ​​联合类型 |​​

    • 变量可多选类型
    ini 复制代码
    typescript
    复制
    let id: string | number;
    id = "001";  // 合法
    id = 100;    // 合法
  2. ​​字面量类型​​

    • 固定值作为类型(常与联合类型配合)
    ini 复制代码
    typescript
    复制
    type Direction = "left" | "right" | "up" | "down";
    let move: Direction = "left"; // 只能赋值指定值
  3. ​​any​​

    • 关闭类型检查(慎用!失去TS优势)
    ini 复制代码
    typescript
    复制
    let unknownData: any = fetchExternalData(); 
  4. ​​Resource(ArkUI特有)​​

    • 引用本地资源文件
    less 复制代码
    typescript
    复制
    Image($r('app.media.logo'))  // 加载resources中的图片

🛡️ ​​三、类型守卫与类型推断​​

  1. ​​类型守卫​​

    • 运行时检查类型(typeof/instanceof)
    typescript 复制代码
    typescript
    复制
    function printId(id: number | string) {
      if (typeof id === "string") {
        console.log(id.toUpperCase()); // 自动识别为string
      } else {
        console.log(id.toFixed(2));    // 自动识别为number
      }
    }
  2. ​​类型别名 type​​

    • 为复杂类型命名
    ini 复制代码
    typescript
    复制
    type UserID = string | number;
    type Callback = () => void;

💎 ​​核心数据类型总结表​​

​​类型​​ ​​示例​​ ​​典型场景​​
number let count: number = 5; 数值计算
string let msg: string = "Hi"; 文本展示
boolean let isDone: boolean = false; 条件判断
Array<T> let list: number[] = [1,2,3]; 数据集合
Interface interface User { id: number } 结构化数据定义
Union Types `let id: string number;`
Resource $r('app.string.title') 本地化资源引用

实际开发建议:

  1. ​优先使用接口定义对象结构​
  2. ​避免 any 充分利用TS静态检查​
  3. ​联合类型+类型守卫提高安全性​
  4. ​善用资源类型加载本地文件​
相关推荐
hqzing8 小时前
把 glibc 移植到鸿蒙 PC 上:它能用来做什么
华为·harmonyos
OH_TPC13 小时前
HarmonyOS APP开发---“习惯猫“习惯打卡App,需要用到这个库
华为·harmonyos·鸿蒙
熊猫钓鱼>_>16 小时前
开源鸿蒙平台 KMP 三方库 kotlin-csv 适配全流程:从编码假设到沙箱真机互通验证
开发语言·人工智能·华为·kotlin·harmonyos·鸿蒙
马剑威(威哥爱编程)18 小时前
【共创稿事节】HarmonyOS 7 Agent 开发实战 09:Agent 测试评测集
华为·harmonyos
威哥爱编程20 小时前
HarmonyOS 7 Agent 开发实战 04:云端 RAG 搭企业私有知识库问答
agent·harmonyos
威哥爱编程20 小时前
HarmonyOS 7 Agent 开发实战 05:流式输出做打字机对话
agent·harmonyos
donk131420 小时前
React Native for OpenHarmony 实战:三方库 react-native-circular-action-menu 的鸿蒙化适配指南
javascript·分布式·react native·react.js·wpf·harmonyos
SuperHeroWu721 小时前
【HarmonyOS 7】 HarmonyOS 应用 Skill创建和编排详解
华为·harmonyos·开放平台·skill·小艺