[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 ...
随机推荐
- git针对Android Studio的使用
1.将文件放到项目根目录下 .gitignore 文件内容: *.iml.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_ ...
- jquery动画总结
基本动画 show() //直接显示元素,没有动画 show(speed, [callback]) //有动画,有回调函数 hide() //直接隐藏元素,没有动画 hide(speed, [call ...
- UITabBarController自定义一
UITabBarController自定义一 首先在Appdelegate.m文件中将UITabBarController的子类设置为rootViewController,并设置其viewContro ...
- asp.net 图片质量压缩(不改变尺寸)
private static ImageCodecInfo GetEncoderInfo(String mimeType) { int j; ImageCodecInfo[] encoders; en ...
- Multiple dex files define Lcom/sina/sso/RemoteSSO错误解决办法
在安卓上遇到了Multiple dex files define Lcom/sina/sso/RemoteSSO的编译错误 在网上找解决办法 搜到了解决办法是这样的 方案1:Eclipse->P ...
- javascript关于原型的深刻理解
Javascript继承机制的设计思想 作者: 阮一峰 日期: 2011年6月 5日 我一直很难理解Javascript语言的继承机制. 它没有"子类"和"父类&qu ...
- jqgrid设置单元格数据
$("#gridid").jqGrid('setCell',rowid,icol,data); rowid为行ID,jqgrid内置的那个,从1开始 icol为列索引,从0开始, ...
- Thinkphp---练习:数据的增删改查
利用ThinkPHP连接数据库的增删改查的例题:用到的数据库表名Info表,Nation表 数据显示页面:MainController.class.php中的方法(增删改查全包括--function ...
- HTml <meta>标签的使用(重要)
<meta> 元素可提供有关页面的元信息(meta-information),比如针对搜索引擎和更新频度的描述和关键词. 1.设置网页字符编码 <meta http-equiv=&q ...
- jquery 插件模版
;(function ($) { //插件的默认值属性 var defaults = { Weight: '300px', height: '230px', nextId: 'nextBtn', ne ...