If Typescript is the first language in which you've encountered generics, the concept can be quite difficult to understand. We skip the lecture in this lesson and dive straight into a real-world use-case that is guaranteed to help you understand the need for generics.

Let's say we have this part of code:

class Emitter{
emit(event){
console.log(event);
}
} const emitter = new Emitter(); emitter.emit({path: '/home', directory: true});

The object we want to pass in is {path: "", directory: ""}. But it may happen that we have some typo error, so we want IDE helps us to detect that.

TO do this, we need interface:

class Emitter<MyEvent>{
emit(event: MyEvent){
console.log(event);
}
} interface MyEvent{
path: string
directory: boolean
} const emitter = new Emitter<MyEvent>(); emitter.emit({path: '/home', directory: true});

So it defines that the emit() function's param should have 'directory' and 'path' attrs. If not, it will report error.

So far so good, but what happen if we have anyother function inside the class, such as:

class Emitter<T>{ // T: allow everything come in
emit(event: MyEvent){
console.log(event);
} yield(value: MyValue){
console.log(value);
}
} interface MyEvent{
path: string
directory: boolean
} interface MyValue{
message: string
} const emitter = new Emitter<MyEvent>();
const yielder = new Emitter<MyValue>(); emitter.emit({path: '/home', directory: true});
yielder.yield({message: "Hello World!"});

yield() take objet with message prop, and the interface defined as MyValue. So allows Emitter class accept multi interfaces, we can use <T>, then for each function, we add the interface for that.

[Typescript] Introduction to Generics in Typescript的更多相关文章

  1. [TypeScript] Custom data structures in TypeScript with iterators

    We usually think of types as something that can define a single layer of an object: with an interfac ...

  2. [TypeScript] The Basics of Generics in TypeScript

    It can be painful to write the same function repeatedly with different types. Typescript generics al ...

  3. [Typescript] Generics using TypeScript

    In this lesson we cover the key reason why programming languages need generics. We then show how use ...

  4. [TypeScript] Understand lookup types in TypeScript

    Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...

  5. 学习TypeScript,笔记一:TypeScript的简介与数据类型

    该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27  16:50:03 一.什么是TypeScript? TypeScript是javasc ...

  6. [Typescript] Specify Exact Values with TypeScript’s Literal Types

    A literal type is a type that represents exactly one value, e.g. one specific string or number. You ...

  7. [TypeScript] Overload a Function with TypeScript’s Overload Signatures

    Some functions may have different return types depending on the types of the arguments with which th ...

  8. [TypeScript] Represent Non-Primitive Types with TypeScript’s object Type

    ypeScript 2.2 introduced the object, a type that represents any non-primitive type. It can be used t ...

  9. [TypeScript] Creating a Class in TypeScript

    Typescript classes make traditional object oriented programming easier to read and write. In this le ...

随机推荐

  1. WinForm窗体之间传值

    当程序需要将一个窗体中的一些信息传给另一个窗体并让其使用时,就需要用到这个知识点 方法一:通过接受参数的窗体的构造函数传值 例:现有Form1和Form2两个窗体,二者都包含一个文本框,Form1还包 ...

  2. 深入理解offsetTop与offsetLeft

    做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框.那么问题来了,包含元素off ...

  3. JavaScript-学习一

    JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getElementbyID 是不同的. 同样,变 ...

  4. 让 zend studio 识别 Phalcon语法并且进行语法提示

    让 zend studio  识别 Phalcon语法并且进行语法提示 https://github.com/phalcon/phalcon-devtools/tree/master/ide 下载解压 ...

  5. Ubuntu16.04下编译vim with python support失败的原因

    - youcompleteme原话:On Ubuntu 16.04, Python support was not working due to enabling both Python2 and P ...

  6. Kafka笔记--指定消息的partition规则

    参数的设定:参考资料 不错的资料:http://blog.csdn.net/honglei915/article/details/37697655 http://developer.51cto.com ...

  7. poj 4982 踩方格

    4982:踩方格 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 有一个方格矩阵,矩阵边界在无穷远处.我们做如下假设:a.    每走一步时,只能从当前方格移 ...

  8. cf C. Bits

    http://codeforces.com/contest/485/problem/C 利用位运算来解决这个题,从L开始,从每一位按位或,知道到达r位置,ans=ans|(1<<k);就可 ...

  9. Scala开启之旅

    嘿嘿,公司最近需要测试SPARK性能,赶上了.. 那LUA之后,SCALA也简单看看,,, 其实,我三月时买了本129元的SPARK的书,,全国只那一本哈. package com.hengheng. ...

  10. EMV规范 ---ISO7816 T=0协议的时间特性

    复位应答期间: 字符间的时间间隔最小是12etu,最大是9600etu,但整个ATR不得超过19200etu(TS的起始沿到最后一个字符的起始沿 从卡片发出的连续字符其最小时间间隔为12etu,但是终 ...