zoukankan      html  css  js  c++  java
  • TypeScript

    1.基本类型

    布尔值

    let isDone: boolean = false;

     字符串

    let name:  string = "bob";

     数字

    let decLiteral:  number = 6;

     数组-可以在元素类型后面接上 [],表示由此类型元素组成的一个数组

    let list: number[] = [1, 2, 3];

    数组-使用数组泛型,Array<元素类型>

    let list: Array<number> = [1, 2, 3];

    数组-元组表示知道长度和类型的数组,当访问越界数组时,类型只能是指定的数组类型

    let x: [string, number];

    any-不清楚类型时,表示任意类型

    let notSure: any = 4;

    void-表示没有任何类型,声明一个void类型的变量没有什么大用,因为你只能为它赋予undefinednull

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

    never-返回never的函数必须存在无法达到的终点

    // 返回never的函数必须存在无法达到的终点
    function error(message: string): never {
        throw new Error(message);
    }
    
    // 返回never的函数必须存在无法达到的终点
    function infiniteLoop(): never {
        while (true) {
        }
    }

    undefinednull-默认情况下nullundefined是所有类型的子类型。 就是说你可以把 nullundefined赋值给number类型的变量

    let u: undefined = undefined;
    let n: null = null;

    object

    function create(o: object | null): void;

    类型断言-两种使用方法,其一是“尖括号”语法,另一个为as语法,使用时知道具体的数据类型的场景

    let someValue: any = "this is a string";
    
    let strLength: number = (<string>someValue).length;
    let strLengthOther: number = (someValue as string).length;

     2.接口

    interface定义接口,接口能够描述JavaScript中对象拥有的各种各样的外形。 除了描述带有属性的普通对象外,接口也可以描述函数类型。

    interface SquareConfig {
      color: string;
      width: number;
    }
    interface SearchFunc {
      (source: string, subString: string): boolean;
    }

    接口继承

    interface Shape {
        color: string;
    }
    
    interface PenStroke {
        penWidth: number;
    }
    
    interface Square extends Shape, PenStroke {
        sideLength: number;
    }

    可选属性-定义接口属性-可以对可能存在的属性进行预定义,可以捕获引用了不存在的属性时的错误

    interface SquareConfig {
      color?: string;
      width?: number;
    }

    只读属性-只能在对象刚刚创建的时候修改其值

    1.
    interface Point {
        readonly x: number;
        readonly y: number;
    }
    2.
    // ReadonlyArray<T>
    let a: number[] = [1, 2, 3, 4];
    let ro: ReadonlyArray<number> = a;
  • 相关阅读:
    JS中的call()和apply()方法和bind()
    reactjs入门到实战(十)----one-first_app
    49-Reverse Linked List II
    48-Merge Sorted Array
    47-Generate Parentheses
    46.Valid Parentheses
    45-Letter Combinations of a Phone Number
    44-Count and Say
    43-Reverse Nodes in k-Group
    42-Remove Nth Node From End of List
  • 原文地址:https://www.cnblogs.com/wangxirui/p/14736200.html
Copyright © 2011-2022 走看看