TypeScript
使用
typescript是编译型,编译成javascript(解释型).
- 安装
npm install -g typescript
- 编译
- 编写react时以.tsx为后缀
强类型
指定类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var str:string = "1"; var num:number = 1; var bool:boolean = true; var un:undefined = undefined; var nil:null = null; var a:any; a = 1; a = "1";
var func1 = function():void{ }
var func2 = function():number{ return 0 }
|
没有赋值,表示是any值,以后不管怎么赋值都是any值的类型
1 2 3
| var a2; a2 = 1; a2 = "1";
|
联合类型,方法只能调用类型公共的方法
1 2 3
| var strnum:string|number = "1";
strnum.toString()
|
接口
类似java的接口,’子类必须实现接口’
1 2 3 4 5 6
| interface Inf{ name:string }
var obj:Inf; obj = {name:"ring"}
|
可选属性
1 2 3 4 5 6 7
| interface Inf{ name:string, age?:number }
var obj:Inf; obj = {name:"ring"}
|
不确定数量属性
1 2 3 4 5 6 7 8
| interface Inf{ name:string, age?:number, [propName:string]:any }
var obj:Inf; obj = {name:"ring", age:10, sex:man}
|
只读属性
1 2 3 4 5 6 7
| interface Inf{ name:string, readonly age:number }
var obj:Inf; obj = {name:"ring", age:10, sex:man}
|
数组
类型方括号
1 2
| var arr:number[] = [1, 2, 3] var arr2:any[] = [1, 2, 3]
|
泛型定义法
1 2
| var arr:Array<number> = [1, 2, 3] var arr2:Array<any> = [1, 2, 3]
|
接口定义法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| interface IArr{ [index:number]:number } var arr:IArr = [1, 2, 3]
interface Istate{ name:string, age:number }
interface IArr{ [index:number]:Istate } var arr:IArr = [{name:"ring", age:20}] var arr:Array<IArr> = [{name:"ring", age:20}] var arr:Istate[] = [{name:"ring", age:20}]
|
函数
声明式函数
1 2 3 4 5 6
| function func(name:string, age:number):number{ return age }
var age:number = func("ring", 20)
|
函数参数不确定
1 2 3 4 5
| function func(name:string, age:number, sex?:string):number{ return age }
var age:number = func("ring", 20, "man")
|
参数默认值
1 2 3 4 5
| function func(name:string, age:number, sex?:string="man"):number{ return age }
var age:number = func("ring", 20, "man")
|
表达式类型函数
1 2 3 4 5
| var func = function(name:string, age:number):number{ return age }
var age:number = func("ring", 20)
|
表达式类型函数变量的约束规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| var func:(name:string, age:number)=>number = function(name:string, age:number):number{ return age }
var age:number = func("ring", 20)
interface Istate{ (name:string, age:number):number } var func:Istate = function(name:string, age:number):number{ return age }
var age:number = func("ring", 20)
|
采用重载的方式支持联合类型的函数关系
1 2 3 4 5 6 7
| function func(value:string):string; function func(value:number):number; function func(value:string|number){ return value } let a:number = func(1) let b:string = func("1")
|
类型断言
在联合类型的情况下, 调用的函数要是共有的函数, 不然会报错, 所以需要进行断言
<类型>值 或 值as类型
1 2 3 4 5 6
| function func(name:string|number){ return (<string>name).length() }
|
类型别名
1 2 3
| type strtype = string|number; var str:strtype = "10" str = 10
|
对于接口
1 2 3 4 5 6 7 8 9 10 11
| interface muchType1{ name:string } interface muchType2{ age:number }
type muchType = muchType1|muchType2 var obj1:muchType = {name:"ring"} var obj2:muchType = {age:1} var obj3:muchType = {name:"ring", age:1}
|
限制字符串的选择
1 2 3 4 5 6
| type sex = "man"|"woman" function func(s:sex):string{ return s } func("man")
|
枚举
使用枚举可以定义一些有名字的数字常量
1 2 3 4 5 6 7 8 9 10
| enum Days{ Sun, Mon, Tue, Wed, Thu, Fri, Sat, } console.log(Days.Sun)
|
typescript枚举出来会自动赋值外, 同时会对枚举值到枚举名的反向映射, 就是变成了双向映射
1 2 3 4 5 6 7 8 9 10 11 12 13
|
var Days; (function (Days) { Days[Days["Sun"] = 0] = "Sun"; Days[Days["Mon"] = 1] = "Mon"; Days[Days["Tue"] = 2] = "Tue"; Days[Days["Wed"] = 3] = "Wed"; Days[Days["Thu"] = 4] = "Thu"; Days[Days["Fri"] = 5] = "Fri"; Days[Days["Sat"] = 6] = "Sat"; })(Days || (Days = {})); console.log(Days.Sun);
|
类的修饰符
public private protected
1 2 3 4 5 6 7 8 9 10 11
| class Person{ name = "ring", private age = 20, protected say(){ console.log("halo") } }
|
类的继承
1 2 3 4 5 6
| class Child extends Person{ callParent(){ super.say() } }
|
静态方法, 可以通过类名调用
1 2 3 4 5 6 7 8 9 10
| class Child extends Person{ callParent(){ super.say() } static test(){ console.log("halo") } }
console.log(Child.test())
|
泛型
泛型是指在定义函数, 接口或类的时候, 不预先指定具体类型, 而在使用的时候再指定类型的一种特性.
也可以帮助我们限定约束规范
1 2 3 4 5 6 7 8 9 10 11
| function func<T>(len:number, value:<T>):Array<T>{ let arr = [] for(var i=0;i<len;1++){ arr[i] = value } return arr; }
var strArry: string[] = func<string>(3, '1') var numArry: number[] = func(3, 1)
|
接口采用泛型
1 2 3 4 5 6 7 8 9 10 11 12 13
| interface Istate{ <T>(name:string, value:T):Array<T> }
let func:Istate;
func = function<T>(name:string, value:T):Array<T>{ return [] }
var strArry:string [] = func("ring", "2020") var numArry:number [] = func("ring", 2020)
|