JHHK

欢迎来到我的个人网站
行者常至 为者常成

数据类型

目录

介绍

boolean类型

// boolean类型
Button('boolean类型').onClick(()=>{
    const isBool: boolean = true
    console.log('lxy:this is',isBool)
})

number类型

// number类型:整数和浮点都是number类型
Button('number类型').onClick(()=>{
    const a:number = 10
    console.log('lxy:a is ', a)

    const b:number = 8.9
    console.log('lxy:a is ', b)
})

string类型

// string
Button('string类型').onClick(()=>{
    const a:string = 'hello'
    const b:string = 'world'
    const c:string = a + ' ' + b
    // 字符串拼接
    console.log('lxy c is ', c)

    // 字符串插值
    console.log(`lxy a is ${a}, b is ${b}, c is ${c}`)
})

Array类型

// Array类型
Button('Array类型').onClick(()=>{
    // 数组声明方式一
    const a:Array<number> = [1, 2, 3]
    // 数组声明方式二
    const b:number[] = [3, 4, 5]
    console.log('lxy a is ', a)
    console.log('lxy b is ', b)
    console.log('----------------------')
    b.forEach((value:number,index:number, array:number[])=>{
    console.log('lxy value is ', value)
    console.log('lxy index is ', index)
    console.log('lxy array is ', array)
    })
})

元组类型

// 元组类型
Button('元组类型').onClick(()=>{
    // 元组内的类型可以不一样
    const a: [number, string] = [10, 'Tom']
    console.log('lxy a[0] is ', a[0], 'a[1] is ', a[1])
})

any类型

Button('any类型(不允许使用了)').onClick(()=>{
    // Use explicit types instead of "any", "unknown" (arkts-no-any-unknown) <ArkTSCheck>
    // any 类型在arkTs中不可用
    // const a: any = 'jack'
    console.log('lxy any 类型在arkTs中不可用');
})

void / null / undefined / never 类型

// void类型
Button('void / null / undefined / never 类型').onClick(()=>{
    //1. null类型只能被赋值null
    const a:null = null;
    console.log('lxy a is',a)

    //2. undefined类型只能被赋值undefined
    const b:undefined = undefined;
    console.log('lxy b is',b)

    // undefined允许被赋值给void类型
    // null不能赋值给void类型
    const c:void = undefined;
    console.log('lxy c is',c)

    //3. never:代表从不会出现的值
    let d:never
    // 这是一个自执行函数,因为执行过程中抛出了异常,所以不会有返回值
    d = (()=>{ throw new Error('exception')})();
    console.log('lxy d is ', d)

    // 返回值为 never 的函数可以是无法被执行到的终止点的情况
    // function error(message:string):never {
    //   throw new Error(message);
    // }

    // function loop(): never {
    //   while (true) {}
    // }
})

对象类型

interface Person {
  name:string
  age:number
}

class Car {
  brand:string;
  price:number;

  constructor(brand:string, price:number) {
    this.brand = brand;
    this.price = price;
  }
}

对象类型的使用

Button('对象类型').onClick(()=>{
    // Object literal must correspond to some explicitly declared class or interface
    // (arkts-no-untyped-obj-literals) <ArkTSCheck>
    // const obj: object = {'name':'tom','age':18}

    const p : Person = {name:'tom', age:18}
    console.log('lxy p.name is', p.name, 'p.age is', p.age)
    // Object literal must correspond to some explicitly declared class or interface (arkts-no-untyped-obj-literals) <ArkTSCheck>
    // 类型不匹配
    // const car:Car = {brand:'BMW',price:100 };
    
    const car: Car = new Car('BMW',100);
    console.log('lxy car.brand is', car.brand, 'car.price is',car.price)
})

Record类型

// Record<K,T> 构造一个对象类型,Keys 表示对象的属性键 、Type 表示对象的属性值,用于将一种类型属性映射到另一种类型
Button('Record类型').onClick(()=>{

    // 通过这种方式,我们可以很容易地统计每个单词在文本中出现的次数,
    // 并且确保每个单词只出现一次作为键,以及对应的出现次数是一个整数。
    type WordCount = Record<string, number>;
    const wordFrequency: WordCount = {
        "apple": 5,
        "banana": 3,
        "orange": 7
    };
    console.log('lxy:apple number is ' + wordFrequency.apple);
})

行者常至,为者常成!





R
Valine - A simple comment system based on Leancloud.