TypeScript是由微软开发开源的JavaScript的一个超集,主要提供了类型系统和对ES6的支持,TypeScript增加了代码的可读性和可维护性
安装
- 全局安装:npm i -g typescript
- 编译文件:tsc hello.ts
- 约定文件以.ts为后缀,编写react时以.tsx为后缀
数据类型
基本类型
- string,number,boolean,null,undefined,enum,symbol
- 空值一般用void表示,void可以表示变量,也可以表示函数无返回值
- 任意值Any,用来表示允许赋值为任意类型,变量在声明时未指定类型并且未赋值,就是任意值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| var str:string = "hello" var num:number = 1 var bo:boolean = true var un:undefined = undefined var nu:null = null var an:any = "1" var an2
an = 1 an2 = true an2 = "str"
str = undefined str = null
var fun = function():void{ }
|
联合类型
- 联合类型表示取值可以为多类型中的一种,只能访问联合类型中共有的属性和方法
1 2 3 4 5
| var t:string|number = "11" t = 1
console.log(t.toString())
|
对象类型
- 接口:可描述的一部分抽象行为,接口中可定义可选属性,只读属性,任意属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
interface Ista { name: string; age: number; }
var obj1: Ista; obj1 = { name: "名字", age: 10 };
interface Ista2 { name: string; age?: number; } var obj2: Ista2; obj2 = { name: "hh" };
interface Ista3 { name: string | number; age?: number; [propName: string]: any; } var obj3: Ista3; obj3 = { name: "xxx", sex: "男", home: "河南" };
interface Ista4 { name: string; readonly age?: number; } var obj4: Ista4 = { name: "张三", age: 20 }; obj4.name = "李四"
|
数组类型
1 2 3
| var arr: number[] = [1, 2, 3]; var arr2: string[] = ["1", "2", "3"]; var arr3: any[] = ["1", 1, true];
|
1 2 3
| var arr: Array<number> = [1, 2, 3]; var arr2: Array<string> = ["1", "2", "3"]; var arr3: Array<any> = ["1", 1, true];
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| interface Istate { name: string; age: number; }
interface IArr { [index: number]: Istate; }
var arrType: IArr = [{ name: "张三", age: 10 }]; var arrType2: Array<Istate> = [{ name: "张三", age: 10 }]; var arrType3: Istate[] = [{ name: "张三", age: 10 }];
|
函数类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| function fun1(name: string, age: number): number { return age; } var ageNum: number = fun1("张三", 19);
function fun2(name: string, age: number, sex?: string): number { return age; } var ageNum2: number = fun2("张三", 19, "女");
function fun3(name: string = "李四", age: number = 10, sex?: string): number { return age; } var ageNum2: number = fun3("张三", 19, "女");
|
1 2 3 4 5 6 7 8 9 10 11
| var fun4: (name: string, age: number) => number = function( name: string = "李四", age: number = 10 ): number { return age; };
interface Ifun { (name: string, age: number): void; } var fun5: Ifun = function(name: string = "李四", age: number = 10) {};
|
1 2 3 4 5 6 7 8 9
| function getValue(value: number): number;
function getValue(value: string): string; function getValue(value: number | string): number | string { return value; } var a: number = getValue(1); var b: string = getValue("1");
|
类型断言
- 语法:<类型>值或者as类型
- 类型断言不是类型转换,不能断言联合类型中不存在的类型
1 2 3 4
| function getAssert(name: string | number) { return (name as string).length; }
|
类型别名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| type strType = string | number | boolean; var str: strType = "10"; str = 10; str = true;
interface muchType1 { name: string; } interface muchType2 { age: number; }
type muchType = muchType1 | muchType2; var obj1: muchType = { name: "张三" }; var obj2: muchType = { age: 10 }; var obj3: muchType = { name: "张三", age: 10 };
type sex = "男" | "女";
function getSex(s:sex): string { return s; } getSex("男");
|
枚举
- 枚举成员会被赋值从0开始递增的数字,同时也会被枚举值到枚举名进行反向映射
- 枚举类型会被编译成一个双向映射的对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| enum Days { Sun, Mon, Tue, Web, Thu, Fri, Sat } console.log(Days.Sun) console.log(Days.Sat) console.log(Days[1] === "Mon");
enum Days2 { Sun = 3, Mon, Tue, } console.log(Days.Sun);
|
类修饰符
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| class Person { name = "张三"; private age = 10; protected sex = "男"; say() { console.log("我的名字:" + this.name); } }
var p = new Person(); p.say(); p.name;
class Child extends Person { eat() { super.say(); console.log(super.sex); } static test(){ console.log("test") } }
var c = new Child(); c.eat();
c.name;
Child.test()
|
泛型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| function getArr<T>(length: number, value: T): Array<T> { let arr = []; for (var i = 0; i < length; i++) { arr[i] = value; } return arr; }
var strArr: string[] = getArr<string>(6, "1");
var numArr: number[] = getArr(6, 1);
interface IgetArr { <T>(name: string, value: T): Array<T>; }
let fun: IgetArr; fun = function(name: string, value: T): Array<T> { return []; };
var s: number[] = fun("张三", 1);
|
相关文章
JavaScript基础
JavaScript高级
Jquery基础
Jquery高级
Bootstrap
Ajax和Json
DataTables
Sweetalert2
zTree使用
Vue