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. Android开发手记(21) 遍历文件夹

    我们在遍历文件夹的时候由于涉及到SD卡相关操作,所以我们需要添加如下权限: <uses-permission android:name="android.permission.WRIT ...

  2. Swift - 19 - 字典的初始化

    //: Playground - noun: a place where people can play import UIKit // 注意: swift中的字典用的也是中括号, 和OC的大括号是不 ...

  3. Linq 实现普通sql中 where in 的功能

    user.ProjectIds 的值是使用逗号分隔的 例如:1,2,3 projectList = (from a in projectList where (user.ProjectIds.Spli ...

  4. windows下安装CI框架

    CI框架是一个非常流行的 mvc框架, CI框架如何安装和使用,在CI中文网已经讲的比较详细了 ,这里记录下几个需要注意的地方. 一. index.php问题 把压缩包下载解压到项目根目录即可运行里面 ...

  5. 【USACO 3.3.1】骑马修栅栏

    [描述] Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个程序,读入 ...

  6. javascript获得浏览器工作区域的大小

    浏览器的窗口发生变化时会执行window.onresize方法,通过这个方法我们可以获得到浏览器工作区域的大小: window.onresize=function(){ bodyHeight = wi ...

  7. jQuery实现按Enter键触发事件?

    按Enter触发 $(function(){ document.onkeydown = function(e){ var ev = document.all ? window.event : e; ) ...

  8. php中文件引入require

    ./ 表示当前层 ../表示向上一层 php中好像不能像asp那样,用 “/” 表示根目录,但可以用$_SERVER['DOCUMENT_ROOT'] 表示网站根目录 引用分为三种: 上级对下级的引用 ...

  9. iOS开发——OC篇&消息传递机制(KVO/NOtification/Block/代理/Target-Action)

     iOS开发中消息传递机制(KVO/NOtification/Block/代理/Target-Action)   今晚看到了一篇好的文章,所以就搬过来了,方便自己以后学习 虽然这一期的主题是关于Fou ...

  10. c++内存管理错误记录

    extern "C" _CRTIMP int __cdecl _CrtIsValidHeapPointer( const void * pUserData ){ if (!pUse ...