The first step is to install TypeScript globallyInstall using npm npm install -g typescript Install using cnpm cnpm install -g typescript Install using yarn yarn global add typescript Step 2 Initialize TypeScriptIn the vscode terminal >> Run build task >> tsc: monitor tsconfig.json Next, we can start our typescript journey~ TypeScript basic data types // Boolean type boolean number type number string type string array type array tuple type tuple enumeration type enum any type void type never type // Boolean type let flag:boolean = true console.log(flag) //true // Number type let num:number = 11234 console.log(num) // 112 // string type let str:string = 'str str str~' let str1:string = `hello this is string ${ num }` // Also supports template strings console.log(str) // str str str~ // Array type // type1 You can add [] after the element type to indicate an array of elements of this type let list:number[] = [1,2,3] let list1:string[] = ['a','b','c'] // type2 uses array generics, Array<element type> let list3:Array<number> = [1,2,3] let list4:Array<string> = ['a','b','c'] //Tuple type Tuple The tuple type allows representing an array of a known number and type of elements, where each element can be of a different type. let x:[ string, number ] let y:[ number, string ] x = [ 1, 'a' ] // error x = [ 'a', 1 ] // true y = [ 1,'a' ] // true // When accessing an out-of-bounds element, a union type is used instead: x[3] = 'yuejie' // success string supports ( string | number ) type x[4] = true // error bool is not ( string | number ) type // enumeration enum Color { blue, red, orange } enum Flag { success = 1, error = 2 } enum Color1 { blue, red = 4, orange, green } let c:Color = Color.red // 0 let result:Flag = Flag.success // 1 let d:Color1 = Color1.orange // 5 let e:Color1 = Color1.blue // 0 // Any type // Avoid strong type language detection when you don't know what value the user dynamically enters. You can use the any type to mark let notSure:any = 4 notSure = 'this is any' // ok notSure = true // ok let list0:any[] = [1,true,'free'] list0[2] = 1 //ok, no type specified // Void type // means no type. When a function does not return a value, you will usually see its return type as void. function user():void { console.log( 'this is void' ) } // no return value function user1 ():number { return 123 } // returns number type let user2:void = undefined | null // useless, can only use undefined and null // Null and Undefined are not explained let u: undefined = undefined; let n: null = null; // The Never never type represents the type of values that never exist. The never type is the return value type of function expressions or arrow function expressions that always throw exceptions or never return values at all. neve = 123 // error // A function returning never must have an unreachable endpoint neve = (() => { throw new Error('err') })() // success function loop(): never { while (true) { } } // PS Today's tutorial ends here for now, and the object type will be updated later This is the end of this article about TypeScript installation and usage and basic data types. For more information about TypeScript basic data types, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: MySql 5.6.36 64-bit green version installation graphic tutorial
>>: Can Docker become the next "Linux"?
First go to the official website to download and ...
1. Display effect: 2, html structure <div clas...
Summary of common operators and operators in java...
[LeetCode] 183.Customers Who Never Order Suppose ...
This article example shares the specific code of ...
Canvas is a new tag in HTML5. You can use js to o...
Table of contents K8S Advanced Features Advanced ...
Chinese Tutorial https://www.ncnynl.com/category/...
Recently, there have been many database-related o...
I have a product parts table like this: part part...
Generally, during the development process, the su...
Friends who have bought space and built websites s...
Recently I was looking at how Docker allows conta...
The downloaded version is the Zip decompression v...
Servermanager startup connection database error R...