• Object: indicates the object type, for example

    let a: object; // Declare variable A as the object type
    let b = {}; // declare b as the object type
    Copy the code

    Normally, we don’t use it that way. Because it doesn’t make sense to declare object or {}. We usually do that

    let a: {name: string.age: number}; // Define an object variable a that contains attributes name and age. Name is of type string and age is of type number. To use object A, you must have attributes name and age
    a = {name: "The Flying Penguin"}; // Only name attribute, error
    a = {name: "The Flying Penguin".age: 18}; / / right
    
    If you want to declare an object variable, only the name attribute is required. The other attributes are optional
    let b: {name: string, [propName: string] :any};
    b = {age: 18.gender: "male"}; // No mandatory name attribute, error
    b = {name: "The Flying Penguin".age: 18}; // There must be a name attribute, correct
    b = {name: "The Flying Penguin", the age:18.gender: "male"}; // There must be a name attribute, correct
    
    // Sets the type declaration for the function structure
    let c: (x: number, y: number) = > number;
    c = function(x, y) {
      return x + y;
    }
    console.log( c(1.1));/ / print 2
    Copy the code
  • Array type: array type, for example

    // Two representations of arrays
    
    // 1. Syntax: keyword variable name: type []
    let a: string[];
    a = ['1'.'2'.'3'];
    
    // 2. Syntax: keyword variable name: Arrary< type >
    let b: Arrary<number>;
    b = [1.2.3.4.5];
    Copy the code
  • Tuple type (new type of TS) : indicates a tuple type. In essence, it is an array of fixed length

    let a: [string.string];
    a = ['hello'.'world']
    Copy the code
  • Enum Type (new type of TS), indicating the tuple type, for example

    enum a {
        spring = 0.// Starts from 0 by default
        summer = 1,
        autumn,
        winter
    }
    console.log(a.spring) // Prints 0. Enumeration values start at 0 by default
    Copy the code

Add: aliases for types, for example

Type myString = string; let a: myString = "hello world"; type myNum = 1 | 2 | 3 | 4 | 5; let b: myNum = 1; Let b: myNum = 2; Let b: myNum = 6; // 6 is not myNum, errorCopy the code