[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 ...
随机推荐
- oracle中使用minus进行数据排除(类似SqlServer except函数)
minus这个集合操作符号的作用是从一个结果集合中减掉另一个结果集中数据,也就是说从一个结果集中去除两个结果集中的共有部分. 下面是一些例子: 这个例子使用minus从第一个结果集中将两个结果集的公有 ...
- plsql 把数据导出成为 .sql文件,记住了
今天上午,同事 提醒我.可以直接把数据导出成 .sql 文件,类似于 反编译.见下图
- CSS样式的优势
为什么使用css样式来设置网页的外观样式呢?下面是一段文字,我们想把“超酷的互联网”.“服务及时贴心”.“有趣易学”这三个短语的文本颜色设置为红色,这时就 可以通过设置样式来设置,而且只需要编写一条c ...
- ASP.NET菜鸟之路之Application小例子
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. Session介绍 Application对象用 ...
- C++函数覆盖的思考
最近碰到一些问题,一开始很难调试和解决,最后发现原来是在基类函数的模板方法中对子类需要重写的函数没有使用virtual,如下 class Base { public: void say(){test( ...
- Apache Commons DbUtils Problem
- extjs之TypeError: d.read is not a function解决方案
在创建如下代码时报出此错:TypeError: d.read is not a function Ext.define('shebyxgl_sheb_model', { extend: 'Ext.da ...
- [Git]Git安装
1.什么是Git Git是一个分布式版本控制/软件配置管理软件, git是用于Linux内核开发的版本控制工具, 与CVS.Subversion一类的集中式版本控制工具不同,它采用了分布式版本库的作法 ...
- 触摸事件 - UIControlEvents
首先,UIControlEvents有这个几种: UIControlEventTouchDown = 1 << 0, // on all touch dow ...
- JAVA基础(1)之hashCode()
JAVA基础(1)之hashCode() 看到一篇关于hashCode的文章(),写的很详细明白,瞬间有种恍然大悟的感觉,所以特地转过来.原文:http://blog.csdn.net/fenglib ...