typescript 基础语法
数据类型
关键字
数据类型
类型 |
关键字 |
示例 |
任意类型 |
any |
let a: any = "1";let b: any = 2 |
数字类型 |
number |
let c: number = 3 |
字符串类型 |
string |
const s:string = "ok" |
布尔类型 |
boolean |
const d: boolean = true; |
数组类型 |
[]/Array |
const e1: Array = [1, 2];const e2: number[] = [1, 2]; |
枚举类型 |
string |
enum Animals {dog, cat, pig,};let g: Animals; |
null类型 |
null |
表示对象值缺失。 |
void类型 |
void |
用于标识方法返回值的类型,表示该方法没有返回值 |
undefined类型 |
undefined |
用于初始化变量为一个未定义的值 |
never类型 |
never |
never 是其它类型(包括 null 和 undefined)的子类型,代表从不会出现的值。 |
元组类型 |
[] |
const e1: Array = [1, 2];const e2: number[] = [1, 2]; |
异常抛出
let x = () => {
throw new Error("这是一个异常");
};
异常捕获
类型断言
- 可以用来手动指定一个值的类型,即允许变量从一种类型更改为另一种类型
let m: string = "123";
let n: number = <number>(<any>m);
console.log(typeof(n))
console.log(n*2);
运算符
let num: number = 1;
num = num + 1 - 1;
num++;
num--;
--num;
++num;
num = num * 10;
num = num / 2;
num += 1;
num -= 1;
num *= 2;
num /= 2;
console.log("常规运算符:");
console.log("原数 ---> ", num);
console.log(">= 0 ---> ", num >= 0);
console.log("!= ---> ", num != 0);
console.log("== ---> ", num == 0);
console.log("&& ---> ", num > 0 && num < 3);
console.log("|| ---> ", num > 0 || num < -1);
console.log("! ---> ", !num);
let num1: number = 2;
console.log("位运算符:");
console.log("& ---> ", num1 & 1);
console.log("| ---> ", num1 | 1);
console.log("~ ---> ", ~num1);
console.log("^ ---> ", num1 ^ 1);
console.log(">> ---> ", num1 >> 1);
console.log(">>> ---> ", num1 >>> 1);
条件与循环语句
let num:number = 0.5
if (num > 1) {
console.log("num > 1");
} else if (0 < num && num < 1) {
console.log("0 < num < 1");
} else {
console.log("num < 0");
}
switch (num) {
case 1:
console.log(1);
break;
case 2:
console.log(2);
break;
default:
console.log(num);
break;
}
let arr: Array<number> = [1, 2, 3, 4, 50];
for (var i = 0; i < arr.length; i++) {
console.log("arr condition-- > ", arr[i]);
}
var index: any;
for (index in arr) {
console.log("arr in -- > ", arr[index]);
}
for (let ele of arr) {
console.log("arr of -- > ", ele);
}
arr.forEach((val_, idx_, arr_) => {
console.log(val_, idx_, arr_);
arr_[0] = 100;
});
arr.every((val_, idx_, arr_) => {
console.log(val_, idx_, arr_);
arr_[0] = 100;
return true;
});
while (index < arr.length) {
console.log("while -- > ", arr[index]);
index++;
if (index > 100) {
break;
} else {
continue;
}
}
for (;;){
console.log("死循环代码")
}
while(true){
console.log("死循环代码Ï")
}
函数
function add(a: number, b: number): number {
return a + b;
}
add(1, 2);
function print(output?: boolean, ...s: string[]): void {
if (output) {
let s_ = s.join("");
console.log(s_);
} else {
null;
}
}
print(true, "123", "women");
let fn = function () {
console.log("匿名函数");
};
fn();
let sub = (a: number, b: number) => console.log(a - b);
sub(10, 3);
function print2(n: number): void;
function print2(s: string): void;
function print2(a: any) {
console.log(a);
}
print2("123");
function print3(a: number | string) {
console.log(a);
}
print3(123);
内置对象
let num2: number = 1;
let num3 = new Number(2);
console.log(num1.toFixed());
console.log(num1.toFixed(2));
console.log(num1.toLocaleString());
console.log(num2.toString(2));
let s1: string = "ok";
let s2 = new String("ok");
console.log(s1.length);
console.log(s1.charAt(1));
console.log(s1.charCodeAt(1));
console.log(s2.concat(s1));
console.log(s1.indexOf("k"));
console.log(s1.lastIndexOf("k"));
console.log(s1.replace("o", "www"));
console.log(s1.toUpperCase());
console.log(s1.toLowerCase());
console.log(s1.slice(1));
let arr1: Array<number> = [1, 2, 3];
let arr2: number[] = [1, 2, 3];
console.log(arr1.length);
arr1.push(4);
console.log(arr1);
console.log(arr1.pop());
console.log(arr1.reverse());
console.log(arr1.concat(arr2));
console.log(arr1.join("-"));
console.log(arr1.map(Math.sqrt));
console.log(arr1.slice(1, 2));
console.log(arr1.sort());
console.log(arr1.toString());
let mp = new Map();
mp.set("name", "jack");
mp.set("age", 1);
console.log(mp);
console.log(mp.size);
console.log(mp.get("name"));
console.log(mp.has("age"));
let tup = [1, "2"];
console.log(tup.keys());
console.log(tup.length);
console.log(tup.reverse());
console.log(tup[0]);
tup.push("2234");
console.log(tup);
变量的作用域
let var_a: number = 1;
function change() {
let var_b: number = 2;
}