“This is the seventh day of my participation in the Gengwen Challenge. For details, see” Gengwen Challenge “.
1. Install and run the TS
Install typescript NPM install typescript -g Install node before installing TS. Check the version of TS. TSC -v My version is 3.6.3 The first step is TSC xxxx. ts and then run the js compiled by TSC with node and then nodexxxx.jsCopy the code
2. Typescript’s seven data types
Typescript has seven data types: String Number Boolean NULL underenum (enum) symbol There are 6 data types in js. String Number Boolean, I am around, I am around We find that typescript has one more enumeration type than JS and we'll talk more about these laterCopy the code
3. Define five basic types
var str: string = "hello"; var num: number = 3; var falg: boolean = true; var un: undefined = undefined; var nul: null = null; // By the example above; Var /let/const+ variable name +":" variable type =" value"Copy the code
4. Null, underpaid is a subset of the string, number and Boolean types
The phrase "I pay, I am paid" means that the string type, number type and Boolean type can be defined as NULL or underpaidCopy the code
var str: string = null; var num: number = undefined; Var falg: Boolean = null; var falg: Boolean = null; console.log(str); //null console.log(num); //undefined console.log(falg); //nullCopy the code
Boolean values are not necessarily true or false
We know from the above code; Null, underpaid is a subset of the string, number, and Boolean type so the value of the Boolean type may be null or undefined or true or falseCopy the code
6. Use of void
Empty values are generally represented by void, which can be modified by paying around, and null. Let sum = function(): void {console.log(" hello "); }; Var num2: void = undefined; var num2: void = null; console.log(num2);Copy the code
7. Any usage
Any is used to indicate that assignment is allowed to be of any type. If you declare a variable as any type, any operation on it will return any type. If a variable is specified as any other type when it is declared, it will be recognized as any type. var num: any = 1; num = true; num = 10; Console. log(num);Copy the code
Joint type “|” 8.
When defined as a union property, you can access only the properties and methods of the union type. When the variable is a number type, you cannot use the methods in number. var num: string | number = 1; num = "hrlo"; console.log(num);Copy the code