TypeScript installation and use and basic data types

TypeScript installation and use and basic data types

The first step is to install TypeScript globally

Install using npm

npm install -g typescript

Install using cnpm

cnpm install -g typescript

Install using yarn

yarn global add typescript

Step 2 Initialize TypeScript

Initialize TypeScript

In the vscode terminal >> Run build task >> tsc: monitor tsconfig.json

Monitoring

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:
  • TypeScript Basic Data Types
  • Learn about TypeScript data types in one article

<<:  MySql 5.6.36 64-bit green version installation graphic tutorial

>>:  Can Docker become the next "Linux"?

Recommend

A brief discussion on this.$store.state.xx.xx in Vue

Table of contents Vue this.$store.state.xx.xx Get...

Native JS to achieve digital table special effects

This article shares a digital clock effect implem...

How to introduce img images into Vue pages

When we learn HTML, the image tag <img> int...

In-depth analysis of MySQL deadlock issues

Preface If our business is at a very early stage ...

Vue uses Amap to realize city positioning

This article shares the specific code of Vue usin...

Some suggestions on Vue code readability

Table of contents 1. Make good use of components ...

Teach you about react routing in five minutes

Table of contents What is Routing Basic use of pu...

Full steps to create a password generator using Node.js

Table of contents 1. Preparation 2. Writing comma...

Analysis of the process of deploying Python applications in Docker containers

Simple application deployment 1. Directory struct...

How to reset MySQL root password

Table of contents 1. Forgot the root password and...

Implementation of Nginx hot deployment

Table of contents Semaphore Nginx hot deployment ...

The difference between shtml and html

Shtml and asp are similar. In files named shtml, s...