zoukankan      html  css  js  c++  java
  • 【TypeScript】TypeScript中的数据类型

    // String 类型
    // 一个保存字符串的文本,类型声明为 string。可以发现类型声明可大写也可小写,后文同理。
    let str: string = 'abcd';
    let str2: String = 'abcd';
    
    // Boolen 类型
    // boolean是 true 或 false 的值
    // 所以 let isBool3: boolean = new Boolean(1) 就会编译报错,因为 new Boolean(1) 生成的是一个 Bool 对象。
    let isBool: boolean = false;
    
    // Number 类型
    let number: number = 10;
    
    // Array 类型
    // 数组是 Array 类型。然而,因为数组是一个集合,我们还需要指定在数组中的元素的类型。
    // 我们通过 Array<type> or type[] 语法为数组内的元素指定类型
    let arr: number[] = [1, 2, 3, 4, 5];
    let arr2: Array<number> = [1, 2, 3, 4, 5];
    let arr3: any[] = ['2323', 2, 3, true, 5];
    
    let arr3: string[] = ['1', '2', '3'];
    let arr4: Array<string> = ['1', '2', '3'];
    
    // Tuple 类型
    // 元组类型允许表示一个已知元素数量和类型的数组,各元素的类型不必相同。
    // 比如,你可以定义一对值分别为string和number类型的元组。
    let x: [string, number];
    x = ['hello', 10]; // OK
    // x = [10, 'hello']; // Error
    
    // Enums 类型
    // 枚举类型用于定义数值集合。
    // 列出所有可用值,一个枚举的默认初始值是0。你可以调整一开始的范围。
    enum People {Student = 3, Teacher, Doctor};
    let role1: People = People.Student;
    let role2: People = People.Teacher;
    let role3: People = People.Doctor;
    console.log(role1, role2, role3) // 3 4 5
    // 可以自己转换成js代码 之后打印一下People
    
    // Any 类型
    // any 是默认的类型,其类型的变量允许任何类型的值。
    let any: any = 10;
    let any2: any[] = [1, '2', false];
    
    // Void 类型
    // JavaScript 没有空值 Void 的概念,在 TypeScirpt 中,可以用 void 表示没有任何返回值的函数。
    function alertName(): void {
        console.log('My name is changdong');
    }
    
    // Null 和 Undefined
    // TypeScript里,undefined和null两者各自有自己的类型分别叫做undefined和null。 
    // 和void相似,它们的本身的类型用处不是很大。
    let u: undefined = undefined;
    let n: null = null;
    // 默认情况下null和undefined是所有类型的子类型。 就是说你可以把null和undefined赋值给number类型的变量。
    // 然而,当你指定了--strictNullChecks标记,null和undefined只能赋值给void和它们各自。 这能避免很多常见的问题。 
    
    // 启用 --strictNullChecks
    let num: number;
    num = 1; // 运行正确
    num = undefined;    // 运行错误
    num = null;    // 运行错误
    
    
    // 也许在某处你想传入一个string或null或undefined,你可以使用联合类型string | null | undefined。
    
    // 启用 --strictNullChecks
    let num2: number | null | undefined;
    num2 = 1; // 运行正确
    num2 = undefined;    // 运行正确
    num2 = null;    // 运行正确
    
    // Never 类型
    // never 是其它类型(包括 null 和 undefined)的子类型,代表从不会出现的值。
    // 这意味着声明为 never 类型的变量只能被 never 类型所赋值,在函数中它通常表现为抛出异常或无法执行到终止点(例如无限循环)。
    let a: never;
    let b: number;
    // a = 123; // 运行错误,数字类型不能转为 never 类型
    a = (() => { throw new Error('never')})(); // 运行正确,never 类型可以赋值给 never类型
    b = (()=>{ throw new Error('exception')})(); // 运行正确,never 类型可以赋值给 数字类型
    // 返回值为 never 的函数可以是抛出异常的情况
    function error(message: string): never {
        throw new Error(message);
    }
    // 返回值为 never 的函数可以是无法被执行到的终止点的情况
    function loop(): never {
        while (true) {}
    }
    
    // Object 类型
    // object表示非原始类型,也就是除number,string,boolean,symbol,null或undefined之外的类型。
    // 使用object类型,就可以更好的表示像Object.create这样的API
    declare function create(o: object | null): void;
    create({ prop: 0 }); // OK
    create(null); // OK
    
    // create(42); // Error
    // create("string"); // Error
    // create(false); // Error
    // create(undefined); // Error
  • 相关阅读:
    进制间转换的理论和实现不同
    操作DOM
    浏览器对象
    浏览器
    面向对象编程——class继承
    面向对象编程——原型继承
    js面向对象编程——创建对象
    JS面向对象编程
    js的JSON
    js标准对象——Date
  • 原文地址:https://www.cnblogs.com/mailyuan/p/13423087.html
Copyright © 2011-2022 走看看