The base type of TS

1. The number of numerical model

let nowDate : number = 2021 
Copy the code

2. The string character

let myname : string = 'Awei' 
Copy the code

3. The Boolean Boolean

let flag : boolean = false
Copy the code

4. Null indicates that the object value is missing

5. Undefined specifies the initialized variable as the defined value

Null and Undefined are subtypes of any other type, including void, and can be assigned to other types, such as numeric types, in which case the assigned type becomes Null or Undefined. With strictNullChecks enabled in TypeScript, null and undefined can only be assigned to void or its own type, as shown in the following example:

// enable --strictNullChecks
let ad : number
ad = 1 / / right
ad = undefined / / error
ad = null / / error
Copy the code

In the example above, the variable x can only be a number. If a type may be null or undefined, | can be used to support a variety of types, the example code is as follows:

// enable --strictNullChecks
let ad : number | null | undefined
ad = 1 / / right
ad = undefined / / right
ad = null / / right
Copy the code

6. Array types

// Add [] to the element type
let arr : number[] = [1.2]

// Or use the array paradigm
let arr : Array<number> = [1.2]
Copy the code

7. Tuples

A tuple type is used to represent an array with a known number and type of elements. The types of each element need not be the same.

let arr : [number.string] 
arr = [1.'str']    / / right
arr = ['str'.1]    / / error
Copy the code

8. Enumeration enum: Defines a set of types

enum Color={Red,Yellow,Blue}
let a:Color = Color:Yellow
console.log(a) / / 1
Copy the code

Void is used to represent the return value of the identity, indicating that this method has no return value

function test:void(){
    console.log('hello')}Copy the code

Any variables declared as any can be assigned any type

let ap : any = 1
ap = 'This is a character.' // AP is a character
ap = 1 // AP is numeric
ap = true // ap is Boolean

Copy the code

11. Never indicates a value that never occurs

The never type is a subclass of other types, including undefined and never, so variables of never can only be assigned by never. Typically represented in functions as throwing an exception or failing to execute to the end (such as an infinite loop).

let x = number 
let y = never
// The error number type cannot be converted to never
y = 123 
// The correct never type can be assigned to the never type
y = (() = >{ throw new Error('err')}) ()// The correct never type can be assigned to a numeric type
x = (() = >{ throw new Error('err')}) ()// A function that returns never can throw an exception
function error( msg : string ) : never {
    throw new Error(msg)
}
// A function that returns never may not be executed to the end
function loop() : never {
    while(true){}s
}
Copy the code

TypeScript Number

MAX_VALUE / / Max
MIN_VALUE / / the minimum
NaN // Non-numeric
NEGATIVE_INFINITY // Negative infinity
POSITIVE_INFINITY // positive infinity
prototype // Number Specifies the static property of the object. Gives you the ability to add properties and methods to objects
constructor// Returns the object that created this objectNumberFunction referenceCopy the code

The prototype instance

function demo(name:string,age:number){
    this.name = name
    this.age = age
}
var demoN = new demo('Awei'.24)
demo.prototype.birth = '2021.08.26'

console.log('s name,+demoN.name) / / name: Awei
console.log('age:+demoN.age) / / age: 24
console.log('birthday:+demoN.birth)  / / birthday: 2021.08.26
Copy the code