Generic Fucntion:

For example we have a set of data and an function:

interface HasName {
name: string;
} const heros: HasName[] = [
{name: 'Jno'},
{name: 'Miw'},
{name: 'Ggr'},
{name: 'Gew'},
{name: 'Wfe'}
]; function cloneArray(ary: any[]): any[] {
return ary.slice();
} const clones = cloneArray(heros);

When we check the 'clones' type, you can see it is 'any[]'.

To add more type information we can change the function:

function cloneArray<T>(ary: T[]): T[] {
return ary.slice();
}

Now we get 'clones' type as 'HasName[]'.

Generic Class:

class SuperCharacter {
constructor(public name: string) { }
} class Hero extends SuperCharacter { } class SuperTeam {
constructor(public members: SuperCharacter[],
public leader: SuperCharacter
) { }
} const captainAmerica = new Hero('Captain America');
const thor = new Hero('Thor');
const ironMan = new Hero('IronMan'); const avengers = new SuperTeam(
[captainAmerica, thor, ironMan],
captainAmerica
); const members = avengers.members;

If we check 'avengers' type is 'SuperTeam'. 'memebers' type is 'SuperCharacter[]'.

To add more information to the types we can do:

class SuperTeam<T> {
constructor(public members: T[],
public leader: T
) { }
}

Now the 'avengers' type is 'SuperTeam<Hero>' and 'members' type is 'Hero[]'.

Now, let's say we have another class:

class Villain extends SuperCharacter {

}

Have some members.

const luthor = new Villain('Luthor');
const bizarro = new Villain('Bizarro');
const captainCold = new Villain('Captain Cold'); const megaCrossoverTeam = new SuperTeam([
captainAmerica, thor, ironMan, luthor,
bizarro, captainCold
], captainAmerica);

'megaCrossoverTeam' is type of 'SuperTeam<Hero | Villain>'.

If we want to add some restrictions to the class, we can do:

class SuperTeam<T extends SuperCharacter> {
constructor(public members: T[],
public leader: T
) { }
}

Then the example below will throw error:

const avengers = new SuperTeam(
[, , ], );

Because the type is number.

[TypeScript] Generic Functions, class, Type Inference and Generics的更多相关文章

  1. [TypeScript] Infer the Return Type of a Generic Function Type Parameter

    When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...

  2. 深入理解typescript的Functions

    Functions Introduction # Functions are the fundamental building block of any application in JavaScri ...

  3. 无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“System.Collections.Generic.List<Ecology.Model.EnergyFlowGraph>”

    无法将类型“System.Collections.Generic.List<anonymous type:string ClassID,string ClsssName>”隐式转换为“Sy ...

  4. Type Safety and Type Inference

    Swift is a type-safe language. A type safe language encourages you to be clear about the types of va ...

  5. Type inference

    Type inference refers to the automatic detection of the data type of an expression in a programming ...

  6. [TypeScript] Union Types and Type Aliases in TypeScript

    Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an ...

  7. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  8. 【区分】Typescript 中 interface 和 type

    在接触 ts 相关代码的过程中,总能看到 interface 和 type 的身影.只记得,曾经遇到 type 时不懂查阅过,记得他们很像,相同的功能用哪一个都可以实现.但最近总看到他们,就想深入的了 ...

  9. Generic method return type

    Here's the Animal class: public class Animal{ private Map<String,Animal> friends =new HashMap& ...

随机推荐

  1. 【Web API系列教程】3.4 — 实战:处理数据(处理实体关系)

    前言 本部分描写叙述了EF怎样载入相关实体的细节,而且怎样在你的模型类中处理环形导航属性.(本部分预备了背景知识,而这不是完毕这个教程所必须的.你也能够跳到第五节) 预载入和延迟载入 预载入和延迟载入 ...

  2. FZU Problem 1853 Number Deletion

    Problem 1853 Number Deletion Accept: 80    Submit: 239 Time Limit: 1000 mSec    Memory Limit : 32768 ...

  3. Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的词汇多样性分析

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-3 @author: guaguastd @name: tw ...

  4. js+ canvas 实现人物走动

    在网上看了一篇管道工玛利亚走动的图片,感觉人物走动的太生涩了,就写了一下代码改动一下: js 代码: //定义数组图片集合 var marios = new Array("image/QQ截 ...

  5. 具体图解 Flume介绍、安装配置

    写在前面一: 本文总结"Hadoop生态系统"中的当中一员--Apache Flume 写在前面二: 所用软件说明: 一.什么是Apache Flume 官网:Flume is a ...

  6. Java之旅--Web.xml解析

    Windows的IIS,是用UI界面进行网站的配置.Linux以下的差点儿全部系统,都是使用配置文件来进行配置,Java容器(JBoss/Tomcat/Jetty/WebSphere/WebLogic ...

  7. 一个JavaWeb项目中使用的部分技术

    -- 2015年8月8日 1. Web框架: Spring+ SpringMVC + MyBatis Spring: 作为容器.工厂,用于解耦以及管理对象生命周期. 整合各类框架和依赖. MVC  : ...

  8. HTML、CSS和JS中注释类型的总结

    添加必要的注释,是一个优秀的程序员必须具备的好习惯,可以大大提高代码的可维护性.可读性. 1.HTML注释 <!--注释的内容--> 注释的地方(根据个人习惯可能有所不同): 结束标签的后 ...

  9. MyEclipse 安装svn 插件步骤详情

    方法一:在线安装 打开HELP- > MyEclipse Configuration Center.切换到SoftWare标签页. 点击Add Site 打开对话框,在对话框Name输入Svn, ...

  10. Redis学习笔记(九) 命令进阶:Pub/Sub(发布/订阅)操作

    原文链接:http://doc.redisfans.com/pub_sub/index.html Redis的Pub/Sub模型可以应对工作中的一些简单应用,涉及到复杂应用还是推荐使用诸如Rabbit ...