[Typescript] Introduction to Generics in Typescript
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的更多相关文章
- [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 ...
- [TypeScript] The Basics of Generics in TypeScript
It can be painful to write the same function repeatedly with different types. Typescript generics al ...
- [Typescript] Generics using TypeScript
In this lesson we cover the key reason why programming languages need generics. We then show how use ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- 学习TypeScript,笔记一:TypeScript的简介与数据类型
该文章用于督促自己学习TypeScript,作为学笔记进行保存,如果有错误的地方欢迎指正 2019-03-27 16:50:03 一.什么是TypeScript? TypeScript是javasc ...
- [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 ...
- [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 ...
- [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 ...
- [TypeScript] Creating a Class in TypeScript
Typescript classes make traditional object oriented programming easier to read and write. In this le ...
随机推荐
- 三、T4模板与实体生成
上文我们最后虽然用模板创建了一个实体类,但是类的内容仍旧是静态的,这里我们需要用动态方式生成类的内容.因为需要查询数据库这里又免不了各种繁琐的连接数据库操作,为了使我们的编码更加直观,仍然采 ...
- 数据库连接未关闭,conn与rs未关闭
场景: 2000多人使用系统,早上打卡签到,时间点比较集中. 程序:会创建connction连接.但是未关闭,导致tomcat挂了.导致连接池已满 解决:conn.close,rs.close.记住一 ...
- ios 改变状态栏颜色
http://stackoverflow.com/questions/17678881/how-to-change-status-bar-text-color-in-ios-7 stackoverfl ...
- mybatis审查要点
1.where条件遗漏情况 <select id="findActiveBlogLike" resultType="Blog"> SELECT * ...
- thinkphp中ajax用户名校验
ajax实在是太神奇了,刚刚接触,不足之处,请大家指正. 采用Ajax方式进行页面无刷新提示,来检测用户名是否存在. 搭建一个thinkphp的环境,在index.html中,ajax代码如下: &l ...
- 分片传输——send和recv函数
最近在写socket编程收发数据,对于如何发送和接收大量数据,一直在思考.send和recv一般缓存区大小为4K,但是如果你要传输的数据超过了这个标准该如何做呢. 我想到的就是如改写write和rea ...
- Python list 常用操作
测试版本: python 2.7 获取第一个.最后一个元素 list1 = ["a", "b", "c"] len1 = len(list1 ...
- 35 Search Insert Position(找到数的位置Medium)
题目意思:在递增数组中找到目标数的位置,如果目标数不在数组中,返回其应该在的位置. 思路:折半查找,和相邻数比较,注意边界 class Solution { public: int searchIns ...
- mybatis中association的column传入多个参数值
顾名思义,association是联合查询. 在使用association中一定要注意几个问题.文笔不好,白话文描述一下. 1: <association property="fncg ...
- sql语句去除重复记录(多表连接的查询)
--处理表重复记录(查询和删除) /********************************************************************************** ...