TypeScript之基础类型

TypeScript 类型:

  1. 布尔类型 (boolean):

    let isDone: boolean = false;
    
  2. 数字类型 (number):

    let decimal: number = 6;
    let hex: number = 0xf00d;
    let binary: number = 0b1010;
    let octal: number = 0o744;
    
  3. 字符串类型 (string):

    let color: string = "blue";
    
  4. 数组类型 (Array):

    let list: number[] = [1, 2, 3];
    
    //使用数组泛型:Array<元素类型>
    let list: Array<number> = [1, 2, 3];
    
  5. 元组类型 ([type1, type2, ...]):

    let x: [string, number];
    x = ["hello", 10];
    
    //当访问一个越界的元素,会使用联合类型替代:
    x[3] = 'world'; // OK, 字符串可以赋值给(string | number)类型
    
  6. 枚举类型 (enum):

    enum Color { Red, Green, Blue }
    let c: Color = Color.Green;
    
  7. 任意类型 (any):

    let notSure: any = 4;
    notSure = "maybe a string instead";
    notSure = false;
    
  8. 空值类型 (void):

    function warnUser(): void {
        console.log("This is my warning message");
    }
    

当一个函数没有返回值时,你通常会见到其返回值类型是 void.
声明一个void类型的变量没有什么大用,因为你只能为它赋予undefined和null

  1. 空类型 (nullundefined):
    let u: undefined = undefined;
    let n: null = null;
    

默认情况下null和undefined是所有类型的子类型。 就是说你可以把 null和undefined赋值给number类型的变量。
当你指定了–strictNullChecks标记,null和undefined只能赋值给void和它们各自。

  1. Never
    // 返回never的函数必须存在无法达到的终点
    function error(message: string): never {
        throw new Error(message);
    }
    // 推断的返回值类型为never
    function fail() {
        return error("Something failed");
    }
    // 返回never的函数必须存在无法达到的终点
    function infiniteLoop(): never {
        while (true) {
        }
    }
    

never类型表示的是那些永不存在的值的类型。 例如, never类型是那些总是会抛出异常或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型; 变量也可能是 never类型,当它们被永不为真的类型保护所约束时。
never类型是任何类型的子类型,也可以赋值给任何类型;然而,没有类型是never的子类型或可以赋值给never类型(除了never本身之外)。 即使 any也不可以赋值给never。

  1. 对象类型 (object):

    let obj: object = { prop: "value" };
    
  2. 联合类型 (type1 | type2):

    let value: string | number;
    value = "hello";
    value = 123;
    
  3. 交叉类型 (type1 & type2):

    interface A { a: string; }
    interface B { b: string; }
    let ab: A & B = { a: "a", b: "b" };
    
  4. 字面量类型:

    let direction: "north" | "south" | "east" | "west";
    direction = "north";
    
  5. 类型别名 (type):

    type Name = string;
    type NameOrNumber = string | number;
    
  6. 接口 (interface):

    interface Person {
        name: string;
        age: number;
    }
    let john: Person = { name: "John", age: 25 };
    
  7. 函数类型 ((param1: type, param2: type) => returnType):

    let myFunc: (x: number, y: number) => number = function(x, y) {
        return x + y;
    };
    
  8. 类型断言 (as<type>):

    let someValue: any = "this is a string";
    //类型断言两种形式 as语法
    let strLength: number = (someValue as string).length;
    //尖括号
    let strLength: number = (<string>someValue).length;
    
  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值