The base type

Boolean value

let isDone: boolean = false;
Copy the code

digital

let decLiteral: number = 6; 
let hexLiteral: number = 0xf00d;
let binaryLiteral: number = 0b1010; 
let octalLiteral: number = 0o744;
Copy the code

string

let name: string = "bob"; 
name = "smith";
Copy the code

An array of

TypeScript manipulates array elements just like JavaScript. There are two ways to define an array. First, the element type can be followed by [] to represent an array of elements of that type:

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

The second way is to use Array generics, Array< element type > :

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

Tuples Tuple

The tuple type allows you to represent an array with a known number and type of elements that need not be of the same type. For example, you can define a pair of tuples of type string and number.

// Declare a tuple type
let x: [string, number];
// Initialize it
x = ['hello', 10]; // OK
// Initialize it incorrectly
x = [10, 'hello']; // Error
Copy the code

The enumeration

Enum types complement the JavaScript standard data types. As in other languages such as C#, enumeration types can be used to give friendly names to a set of values.

enum Color {Red, Green, Blue}
let c: Color = Color.Green;
Copy the code

By default, elements are numbered from 0. You can also manually specify the values of the members. For example, let’s change the above example to number from 1:

enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
Copy the code

Alternatively, all manual assignments are used:

enum Color {Red = 1, Green = 2, Blue = 4}
let c: Color = Color.Green;
Copy the code
enum Color {Red = 1, Green, Blue} let colorName: string = Color[2]; console.log(colorName); // Display 'Green' because its value is 2 in the code aboveCopy the code

Void

In a way, void is like the opposite of any; it means there is no type at all. When a function returns no value, you usually see the return type void:

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

Declaring a void variable doesn’t do much good, because you can only assign undefined and null to it:

let unusable: void = undefined;
Copy the code

function

let IdGenerator: (chars: string, nums? : number) => string; function createUserId(name: string, id: number): string { return name + id; } IdGenerator = createUserId;Copy the code

interface

interface Person { readonly name: string; age? : number; }Copy the code

The read-only attribute is used to restrict the value of an object to being changed only when it is newly created. TypeScript also provides ReadonlyArray

, which is similar to Array

except that all mutable methods are removed, ensuring that arrays can never be modified after they are created.

let a: number[] = [1, 2, 3, 4];
let ro: ReadonlyArray<number> = a;
ro[0] = 12; // error!
ro.push(5); // error!
ro.length = 100; // error!
a = ro; // error!

Copy the code

Any attribute

interface Person { name: string; age? : number; [propName: string]: any; } const p1 = { name: "semlinker" }; const p2 = { name: "lolo", age: 5 }; const p3 = { name: "kakuqo", sex: 1 }Copy the code

The difference between interface and type aliases

extend

Implements

class

Class Greeter {// Static cname: string = "Greeter"; // Member attributes greeting: string; Constructor (message: string) {this. Greeting = message; } // Static getClassName() {return "Class name is Greeter"; } // The greet() {return "Hello, "+ this. Greeting; } } let greeter = new Greeter("world");Copy the code

So what’s the difference between member properties and static properties, and member methods and static methods? Without further explanation, let’s go straight to the compiled ES5 code:

"use strict"; Var Greeter = /** @class */ (function () {var Greeter(message) {this. Greeting = message; } // Static method greeter.getClassName = function () {return "Class name is Greeter"; }; Greeter.prototype. Greet = function () {return "Hello, "+ this. Greeting; }; // Static attribute greeter. cname = "Greeter"; return Greeter; } ()); var greeter = new Greeter("world");Copy the code

ECMAScript private fields

accessor