来给大家上一个typescript快速了解的的小文章。
本人学习的随笔,希望对大家有用。
Typescript基础类型
string number boolean null undefined void
never enum any
Typescript变量声明
var a:sting 在类型声明的时候可以限定 声明对象被赋值的类型。
var a:()=>string 这句话的意思是声明一个返回值类型为字符串的函数。
Typescript变量声明
function(a:sting,b:number,c:any):void{
console.log(a);
console.log(b);
console.log(c);
}
这段代码的意思是声明两个个确定参数,一个不确定参数返回值为空的函数
Typescript关于数组
var arr:string[] 只能赋值字符串类型
let arr:any[] 可以赋值任意类型的数组项
const arr:any[][] 申明了一个二维数组
Typescript关于元组
没有添加数据类型限制的就是元组
Typescript的联合赋值
var a:sting|number|(()=>number)
这段话的意思是声明了一个a,它可以被赋值字符串,数字,和返回数字类型的一个函数
Typescript的接口
interface AnimalInterface{
a:sting,
b:number,
do:()=>void
}
这段话的意思是声明了一个接口。被赋值的对象如果套用这个接口那么结构应该一致。
具体使用如下
var a:AnimalInterface = {
a:"爱情",
b:3,
c:()=>{
console.log(this.a+this.b)
}
}
接口和class一样都是可以继承的
比如
interface IParent1 {
v1:number
}
interface IParent2 {
v2:number
}
interface Child extends IParent1, IParent2 { }
Typescript的class
class a{
public a:sting; 可以通过.访问
pravite b:number; 不可以通过. 访问,只能通过函数或其他方法间接使用
statice:c;//装在了类上 也就是传统javascript的构造函数上
constructer(){
this.a = '爱情';
this.b = 33;
}
drop(){}
}
关于继承和重写
super 关键字。 当函数使用的时候。将this指向父类实例化的对象。
当成对象使用的时候。super 指向父类的prototype对象
Typescript的namespace 命名空间
将 namespace{}理解成(()=>{})()就可以了,也就是一个匿名自执行的函数
Typescript的模块化
export interface IShape {
draw();
}
Circle.ts 文件代码:
import shape = require("./IShape");
export class Circle implements shape.IShape {
public draw() {
console.log("Cirlce is drawn (external module)");
}
}
看上去像commonjs与es6标准的混合使用
可以这么理解吧没有es6的时候,typescript就存在了。那时候必须借助node的模块引用
Typescript的declare
declare是用来干嘛的呢。和javascript的var类似,本质上是一种typescript编译阶段使用的检查。帮助typescript识别第三方插件的使用格式是否正确。
declare var jQuery: (selector: string) => any;
后面的东西我理解成interface的缩写