TypeScript笔记[5]泛型

 

在C++、C#、Java等主流编程语言中,一般对泛型编程提供了支持。合理利用泛型,可以提高开发效率、提升代码质量。

例如在C++编程语言中,常常利用下面的结构表示一个链表的结点:

template<typename T>
struct Node
{
T data;
Node *next;
};

在TS中,也提供了泛型的支持。下面介绍一下TS中的泛型函数与泛型类。

一、泛型函数

function Func<T>(a: T): T {
return a;
}

上面的代码中,<T>表示一个预设的类型,当Func<T>接受一个具体的参数时,会根据参数的类型产生对应版本的函数。例如,在Func(1)中Func<T>会作为:

function Func(a: number): number {
return a;
}

而在Func("魏晋风度")中Func<T>会作为:

function Func(a: string): string {
return a;
}

二、泛型类型

变量的类型也可以是泛型的:

function identity<T>(arg: T): T {
return arg;
} var myIdentity: <U>(arg: U) => U = identity;

三、泛型类

泛型函数将<T>放在函数名之后,泛型类的<T>也放在类型之后。我们来看一个来自官网的例子:

class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
} var myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };

这个例子中,用number将一个泛型类实例化。

interface IDictionary {
add(key: string, value: any): void;
remove(key: string): void;
containsKey(key: string): bool;
keys(): string[];
values(): any[];
} class Dictionary { _keys: string[] = new string[];
_values: any[] = new any[]; constructor(init: { key: string; value: any; }[]) { for (var x = 0; x < init.length; x++) {
this[init[x].key] = init[x].value;
this._keys.push(init[x].key);
this._values.push(init[x].value);
}
} add(key: string, value: any) {
this[key] = value;
this._keys.push(key);
this._values.push(value);
} remove(key: string) {
var index = this._keys.indexOf(key, 0);
this._keys.splice(index, 1);
this._values.splice(index, 1); delete this[key];
} keys(): string[] {
return this._keys;
} values(): any[] {
return this._values;
} containsKey(key: string) {
if (typeof this[key] === "undefined") {
return false;
} return true;
} toLookup(): IDictionary {
return this;
}
}
var persons = new PersonDictionary([
{ key: "p1", value: { firstName: "F1", lastName: "L2" } },
{ key: "p2", value: { firstName: "F2", lastName: "L2" } },
{ key: "p3", value: { firstName: "F3", lastName: "L3" } }
]).toLookup();

TypeScript笔记[5]泛型+Dictionary 转的更多相关文章

  1. 《CLR via C#》读书笔记 之 泛型

    第十二章 泛型 2014-06-15 初始泛型 12.3 泛型基础结构 12.3.1 开放类型与封闭类型 12.3.2 泛型类型和继承 12.3.3 泛型类型同一性 12.3.4 代码爆炸 12.6 ...

  2. 自定义一个可以被序列化的泛型Dictionary<TKey,TValue>集合

    Dictionary是一个键值类型的集合.它有点像数组,但Dictionary的键可以是任何类型,内部使用Hash Table存储键和值.本篇自定义一个类型安全的泛型Dictionary<TKe ...

  3. TypeScript完全解读(26课时)_6.TypeScript完全解读-泛型

    6.TypeScript完全解读-泛型 创建实例ts文件generics.ts 在index.ts内引入 fill是填充数组,创建的数组的元素数是times,填充的值就是接收的value的值 这里传入 ...

  4. 十分钟教你理解TypeScript中的泛型

    转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者.原文出处:https://blog.bitsrc.io/understanding-generics-in-t ...

  5. 收集数据至泛型Dictionary

    开发时,我们经常使用到泛型,不管是List<T>,还是Dictionary<T,V>,下面Insus.NET在测试一些功能,当使用到一些临时数据集时,有好几种方法把数据收集至D ...

  6. c#重点[集合类型]异常,数组,集合ArrayList,List<>,hashTable,hashtable泛型(Dictionary)

    1.foreach[对一些数组或集合进行遍历] foreach(类型 变量名 in 集合对象){语句体} //定义一个数组 ,,,,, }; foreach(var i in sNum1) { Con ...

  7. C#&java重学笔记(泛型)

    C#部分: 1.泛型的出现主要用于解决类.接口.委托.方法的通用性,通过定义泛型类.接口.委托.方法,可以让不同类型的数据使用相同运算规则处理数据,方便了开发. 2.利用System.Nullable ...

  8. 泛型Dictionary的用法详解

    泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的 ...

  9. .Net 笔记(二) 泛型和集合

    前言: 本文中介绍 泛型和集合的区别.也算是自己的一个知识点的回顾,并且把它们写在自己的笔记中. 1.集合: 在讲到集合之前,我们先来回顾下数组的知识点吧,因为集合和数组的关系也是比较微妙的各有利弊, ...

随机推荐

  1. 如何从 Xcode 控制台输出 JavaScript 的 log?

    调试 UIWebView 中的 JavaScript 一直以来都是很痛苦的一件事.通常我们会通过下面的方法调试 HTML 和 JavaScript. 1.第一种,使用桌面浏览器调试.大多数现代浏览器都 ...

  2. Jsp中获得集合List或Set的长度

    首先要引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ...

  3. 【POJ1113】Wall(凸包)

    [题目] Description Once upon a time there was a greedy King who ordered his chief Architect to build a ...

  4. 【HDU1538】A Puzzle for Pirates(经典的海盗问题)

    [题目] Description A bunch of pirates have gotten their hands on a hoard of gold pieces and wish to di ...

  5. Android开源项目发现--- 工具类Log篇(持续更新)

    1.Catlog 手机端log查看工具,支持不同颜色显示.关键字过滤.级别过滤.进程id过滤.录制功能等 项目地址:https://github.com/nolanlawson/Catlog 在线演示 ...

  6. lc面试准备:Implement Queue using Stacks

    1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...

  7. bzoj1816

    这道题不是很难,二分答案+判定即可 注意在一套牌中Joker只能用一次 ..] of longint;     mid,l,r,n,m,i,ans:longint; function check(x: ...

  8. Apache CloudStack多个跨站脚本漏洞(CVE-2013-2136)

    漏洞版本: Apache Group CloudStack 4.1.0 Apache Group CloudStack 4.0.2 Apache Group CloudStack 4.0.1-incu ...

  9. (转载)mysql group by 用法解析(详细)

    (转载)http://blog.tianya.cn/blogger/post_read.asp?BlogID=4221189&PostID=47881614 mysql distinct 去重 ...

  10. php操作Memcache示例

    <?php //==============================实例化============================ $mem=new Memcache; //====== ...