1, Define Boolean type (Boolean)
var flag:boolean = true
console.log(flag) //true
Copy the code
2. Define the number type.
var num:number = 123
console.log(num) //123
Copy the code
3, Define string type (string)
var str:string = '456'
console.log(str) //'456'
Copy the code
The first method defines an array type
Var arr:number[] = [1,23,45,67] console.log(arr)Copy the code
The second way is to define an array type
var arr:Array<string> = ["html","css","js","ts"]
console.log(arr)
Copy the code
6. Define tuple types
var arr:[number,string] = [1,'php']
console.log(arr)
Copy the code
7, Define enumeration type (enum)
// enum Flag {success=1,error=-1} // console.log(Flag.success) 1 // enum Color {red,blue,orange} // Console. log(color.red) 1(if no value is assigned, // enum Color {red,blue=4,orange} // console.log(color.red) 0 // console.log(color.orange) 5 Increment by 1 if no value is assigned.Copy the code
8. Any type (any)
var num:any=123
num='str'
num=true
console.log(num)
Copy the code
Any type of use
var oBox:any = document.getElementById('box')
oBox.style.color = 'red'
Copy the code
Null and undefined subtypes of other data types
//var num:number; //console.log(num) returns undefined; var num:undefined; The console. The log (num) output is undefined, right / / definition is undefined var num: no assignment number | undefined console. The log (num) / / one element may be a number types, May be null, undefined var num: number | null | undefined num = 1234 the console log (num)Copy the code
Void in typescript identifies no type and is generally used to define methods with no return value
/ / / es5 definition method/function the run () {/ / console log (' run ') / /} / / the run (); Function run():void{console.log('run')} run(); Function run():number{return 123} run();Copy the code
The never type is a subtype of other types (including null and undefined), representing values that never occur. This means that variables declaring never can only be assigned by never
var a:undefined; a=undefined; var b:null; b=null; Var a: never a = 123 / / an Error a = (() = > {throw new Error (' Error ')}) ()Copy the code