[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 ...
随机推荐
- eclipse打包jar时包含第三方jar包的相关问题
我用的是mars4.5版本的eclipse 需求:要把写好的工程打成jar包,并能直接运行.工程用了若干个第三方jar. 在打包的时候,eclipse提供的打包方法不能引用第三方jar包,导致了出现C ...
- Hive学习之一 《Hive的介绍和安装》
一.什么是Hive Hive是建立在 Hadoop 上的数据仓库基础构架.它提供了一系列的工具,可以用来进行数据提取转化加载(ETL),这是一种可以存储.查询和分析存储在 Hadoop 中的大规模数据 ...
- C#.net 货币格式转换
/// <summary> /// 输入Float格式数字,将其转换为货币表达方式 /// </summary> /// <param name="ftype& ...
- JavaScript错误处理
JavaScript 错误 - Throw.Try 和 Catch JavaScript 测试和捕捉 try 语句允许我们定义在执行时进行错误测试的代码块. catch 语句允许我们定义当 try 代 ...
- 浅谈Android序列化
序列化原因 序列化的原因基本可以归纳为以下三种情况: 永久性保存对象,保存对象的字节序列到本地文件中: 对象在网络中传递: 对象在IPC间传递. --- --- 序列化方法 在Android系统中关于 ...
- Linux——搭建PHP开发环境第二步:PHP
原文链接:http://www.2cto.com/os/201511/450258.html ##### PHP 编译安装 #### [root@localhost ~]# yum install l ...
- 粗看C#委托
C#的好多定义跟C艹不太相同,先来分析一下“委托”. 1. 委托的定义: 委托,可以认为是类型安全的函数指针,类型安全就是指明确定义了返回类型与参数类型,在C#代码编译时就能够确保指针传参时的安全性. ...
- matlab图像类型转换以及uint8、double、im2double、im2uint8和mat2gray等说明
转自:http://blog.csdn.net/fx677588/article/details/53301740 1. matlab图像保存说明 matlab中读取图片后保存的数据是uint8类型( ...
- Extjs4.1.x使用Application动态按需加载MVC各模块
我们知道Extjs4之后提出了MVC模块开发,将以前肥厚的js文件拆分成小的js模块[model\view\controller\store\form\data等],通过controller拼接黏合, ...
- Python使用Pygame.mixer播放音乐
Python使用Pygame.mixer播放音乐 frequency这里是调频率... 播放网络中的音频: #!/usr/bin/env python # -*- coding: utf-8 -*- ...