博客
关于我
TypeScript知识点简单汇总
阅读量:327 次
发布时间:2019-03-04

本文共 3798 字,大约阅读时间需要 12 分钟。

数据类型

/** * 数据类型 */let s: string;let n: number;let b: boolean;// 数组let arr1: string[] = ['a', 'b', 'c'];let arr2: Array
= [1, 2, 3, 4, 5];let tuple: [string, number] = ['a', 1]; // 元祖let arr3: (string|number)[] = ['a', 1];let anyList: any[] = ['a', 1, true];// 枚举enum Directions{ left=2, right, top, bottom}console.log(Directions.top);console.log(Directions[2]);// 对象interface LabelledValue { label: string;} function printLabel(labelledObj: LabelledValue) { console.log(labelledObj.label);} let myObj = {size: 10, label: "Size 10 Object"};printLabel(myObj);// 函数interface SearchFunc{ (source: string, subString: string): boolean}let mySearch: SearchFunc;mySearch = function(source:string, subString: string): boolean{ let result = source.search(subString); return result>-1;}// 其它方式interface Person { age:number } interface Musician extends Person { instrument:string } var drummer =
{};

函数:

function add(x: number, y: number): number{    return x+y;}let myAdd = function(x: number, y: number): number{    return x+y;}// 函数的完整类型let myAdd_: (baseValue: number, increment: number) => number = function(x: number, y: number): number{    return x+y;}

class Person{    name: string; // 属性必须赋值或者在构造函数中初始化    age: number;    constructor(name: string, age: number){        this.name = name;        this.age = age;    }    // 方法必须实现    getName(): string{        return this.name;    }}

抽象类:

abstract class Animal{    abstract type: string; // 抽象属性只需定义,无需实现(赋值或者在构造函数中初始化)    name: string; // 普通属性定义同一般类    constructor(name: string){        this.name = name;    }    abstract getType(): string; // 抽象方法只需定义,无需实现    getName(): string{ // 普通方法定义同一般类        return this.name;    }    getTheType(): string{        return this.type; // 普通方法中可以包含抽象属性    }}class Dog extends Animal{    type: string; // 类中必须实现所继承的抽象类中的属性    constructor(name: string, type: string){        super(name);        this.type = type;    }    // 类中必须实现所继承的抽象类中的方法    getType(): string{        return this.type;    }}

接口

// 属性接口interface LabelledValue {    label: string;}  function printLabel(labelledObj: LabelledValue) {    console.log(labelledObj.label);}  let myObj = {size: 10, label: "Size 10 Object"};printLabel(myObj);// 函数接口interface SearchFunc{    (source: string, subString: string): boolean}let mySearch: SearchFunc;mySearch = function(source:string, subString: string): boolean{    let result = source.search(subString);    return result>-1;}// 可索引类型接口interface StringArray{    [index: number]: string;}let myArray: StringArray = ["Bob", "Fred"];let myStr: string = myArray[0];// 类接口interface Girl{    // 接口中的属性和方法只需定义,无需实现    name: string;    age: number;     loveYou():void;}class GrilFirend implements Girl{    // 接口中的属性和方法必须实现    name: string;    age: number;    constructor(name: string, age: number){        this.name = name;        this.age = age;    }     loveYou():void{        console.log("Her name is " + this.name + ", her age is " + this.age);    }}let gf = new GrilFirend("Linda", 28);gf.loveYou();

泛型

注意,无法创建泛型枚举和泛型命名空间。

类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。

// 泛型函数function showData
(person: T): T{ return person;}showData
('Lucy');// 泛型类型1interface GenericIdentityFn {
(arg: T): T;}function identity
(arg: T): T { return arg;}let myIdentity: GenericIdentityFn = identity;// 泛型类型2interface GenericIdentityFn
{ (arg: T): T;}function identity
(arg: T): T { return arg;}let myIdentity: GenericIdentityFn
= identity;// 泛型类// 类有两部分:静态部分和实例部分。 泛型类指的是实例部分的类型,所以类的静态属性不能使用这个泛型类型。class GenericNumber
{ zeroValue: T; add: (x: T, y: T) => T;}let myGenericNumber = new GenericNumber
();myGenericNumber.zeroValue = 0;myGenericNumber.add = function(x, y) { return x + y; };

 

转载地址:http://movq.baihongyu.com/

你可能感兴趣的文章
msbuild发布web应用程序
查看>>
MSB与LSB
查看>>
MSCRM调用外部JS文件
查看>>
MSCRM调用外部JS文件
查看>>
MSEdgeDriver (Chromium) 不适用于版本 >= 79.0.313 (Canary)
查看>>
MsEdgeTTS开源项目使用教程
查看>>
msf
查看>>
MSSQL数据库查询优化(一)
查看>>
MSSQL数据库迁移到Oracle(二)
查看>>
MSSQL日期格式转换函数(使用CONVERT)
查看>>
MSTP多生成树协议(第二课)
查看>>
MSTP是什么?有哪些专有名词?
查看>>
Mstsc 远程桌面链接 And 网络映射
查看>>
Myeclipse常用快捷键
查看>>
MyEclipse更改项目名web发布名字不改问题
查看>>
MyEclipse用(JDBC)连接SQL出现的问题~
查看>>
mt-datetime-picker type="date" 时间格式 bug
查看>>
myeclipse的新建severlet不见解决方法
查看>>
MyEclipse设置当前行背景颜色、选中单词前景色、背景色
查看>>
Mtab书签导航程序 LinkStore/getIcon SQL注入漏洞复现
查看>>