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. 【ASP】在特定的范围内产生N个不同的随机数

    ASP产生一个随机数不难.就两条特定语句: <% Randomize x=int(20*rnd+1) %> 以上的两条语句.表示从1~20这个范围内产生随机数,而且这些随机数都是整数. 那 ...

  2. HDU 1160 FatMouse&#39;s Speed DP题解

    本题就先排序老鼠的重量,然后查找老鼠的速度的最长递增子序列,只是由于须要按原来的标号输出,故此须要使用struct把三个信息打包起来. 查找最长递增子序列使用动态规划法.主要的一维动态规划法了. 记录 ...

  3. Android 利用TimerTask实现ImageView图片播放效果

    在项目开发中,往往 要用到图片播放的效果.今天就用TimerTask和ImageView是实现简单的图片播放效果. 当中,TimerTask和Timer结合一起使用.主要是利用TimerTask的迭代 ...

  4. 0x21 剪枝

    这一章真是心态崩,剪枝太玄学啦,特别是那个搜索顺序我靠真的... poj1011 枚举答案,搜索记录当前到第几根木棒. 剪枝:1.从大到小排序 2.排除等效,这个感觉还行,就是木棒按大小顺序进去,去除 ...

  5. page template in kentico

    Ad-hoc templates are used for one page only, for which they were created - this is why they are not ...

  6. mysqli数据库操作简单实例

    mysqli数据库操作简单实例 代码 结果

  7. python spark kmeans demo

    官方的demo from numpy import array from math import sqrt from pyspark import SparkContext from pyspark. ...

  8. java格式化时间到毫秒

    转自:https://blog.csdn.net/iplayvs2008/article/details/41910835 java格式化时间到毫秒: SimpleDateFormat formatt ...

  9. CADisplayLink & NSTimer

    屏幕刷新与UI更新不同步:屏幕刷新由硬件(+GPU)保证,UI更新由软件(CPU保证). 出现卡顿的原因是软件的计算速度跟不上硬件的刷新速度. 一 简介 1 所在框架 CADisplayLink和其它 ...

  10. Mac-O文件加载的全过程(一)

    在Mac的开发中, 有没有想过当我们点击可执行文件之后,Mac究竟做了什么事情才让我们的程序运行起来? 对于应用层开发人员或者普通的用户而言, 其实无需知道的这么详细:但是对于内核开发人员而言, 如果 ...