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

MySQL 8.0.15 installation tutorial for Windows 64-bit

First go to the official website to download and ...

Summary of uncommon operators and operators in js

Summary of common operators and operators in java...

SQL implementation of LeetCode (183. Customers who have never placed an order)

[LeetCode] 183.Customers Who Never Order Suppose ...

JavaScript to achieve progress bar effect

This article example shares the specific code of ...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...

Basic tutorial on controlling Turtlebot3 mobile robot with ROS

Chinese Tutorial https://www.ncnynl.com/category/...

Super detailed MySQL usage specification sharing

Recently, there have been many database-related o...

A brief talk about MySQL pivot tables

I have a product parts table like this: part part...

Solve the problem of docker container exiting immediately after starting

Recently I was looking at how Docker allows conta...

MySql5.7.21 installation points record notes

The downloaded version is the Zip decompression v...

How to solve the error of connecting to the database when ServerManager starts

Servermanager startup connection database error R...