Install Node.js environment
https://nodejs.org/en
The version number can be found in the terminal, which means the installation is successful.
Then, Terminal executes npm i typescript -g
Install TypeScript.
If the version number is found, the installation is successful.
string type
let str:string = "Hello";
console.log(str);
Execute first in the terminal tsc --init
, then execute tsc -w
. It was found that there was only index.ts in the original TS folder, but now there are two more files.
Open another terminal and execute
to output.
Template strings are also supported:
let num:number = 12
let str:string = `${
num}`
console.log(str);
number type
let notANumber: number = NaN;//Nan
let num: number = 123;//普通数字(包括整型和浮点型)
let infinityNumber: number = Infinity;//无穷大
let decimal: number = 6;//十进制
let hex: number = 0xf00d;//十六进制
let binary: number = 0b1010;//二进制
let octal: number = 0o744;//八进制s
Boolean type
// let boolean0:boolean = new Boolean(1) // 此时是对象类型,不是 boolean 类型,要写为以下形式
let createdBoolean: Boolean = new Boolean(1)
let boolean1: boolean = true //可以直接使用布尔值
let boolean2: boolean = Boolean(1) //也可以通过函数返回布尔值
Null and undefined types
let u: undefined = undefined;//定义undefined
let n: null = null;//定义null
void type
let v1:void = null
let v2:void = undefined
// 没有返回值的函数(非严格模式)
function fn(): void {
return
}
let v1:void = null
There may be errors, you need to set to tsconfig.json
in , close the strict mode, no more errors. Moreover, in non-strict mode, and can be assigned to each other.strict
false
null
undefined
void
Types are not assignable to other types, and null
types undefined
are assignable to other types. But in strict mode, null
you cannot assign to void
the type.