Basic types in TS

Boolean type

Let variable name: Data type = value Let Flag: Boolean = trueCopy the code

Number type: Decimal => 10, binary => 0B1100, octal => 0O12, hexadecimal => 0xA

let a:number = 10
Copy the code

String type

Let a:string ="1" let a:string ="1Copy the code

Undefined and null

let u:undefined = undefined
Copy the code

An array type

  1. To define an array
Let arr: number [] = [1, 2, 3]Copy the code
  1. The generic definition
Let arr: Array < number > = [4] 2Copy the code
Note: After the array type is defined, the array value must be the same as the type
  1. A tuple type
let arr:[string,number,boolean] = ['hi',2,true]

arr[0].toString()  
arr[1].tofiexd()
Copy the code

Enumerated type

enum color{Red,Green,Blue}
Copy the code

Any type: Any type can be declared

let arr:any[] = [100,'hello',true]
Copy the code

Void type

Function fn():void{} // no return valueCopy the code

Object, non-primitive type, except for number, string, and Boolean

Function fn2(obj:object):object{} // Return objectCopy the code

Union type: The flag value can be one of multiple types

function toString(x:number | string):string{}
Copy the code

Type assertion: The type of a value can be manually specified

function str(x:number| string){
    if((<string>x).length){
        return (x as string).length
    }
}
Copy the code

Interface: An abstraction (description) of an object’s state (properties) and behavior (methods)

interface Iperson{ readonly id:number; // Keyword readonly Read-only name:string; // String type sax? :string // Keyword? } let person:Iperson = {id:1, name:' kakashi ', sax:' optional '}Copy the code

Function types: Each parameter in the argument list needs a name and type

interface SearchFunc{
    (source:string,subString:string):boolean
}

const maySearch:SearchFunc = function(source:string,subString:string):boolean{
}
Copy the code