[TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the property keys of an object. We'll use the function spyOn from Jest to illustrate how lookup types can type-safe function parameters.
Considering this code:
function spyOn(obj: Object, prop: string) {
console.log(obj, prop);
}
interface IPerson {
name: string,
age: number
}
const person: IPerson = {
name: 'John',
age: 54
};
spyOn(person, 'address');
We have a 'IPerson' interface and we spyOn 'person' object for 'address' prop. IDE cannot catch any error.
If we want IDE helps to catch error, we can use generics for 'spyOn' function:
function spyOn<O extends object, P extends keyof O>(obj: O, prop: P) {
....
}
So what we tell TypeScript is,
- First param is an object,
- Second param is a prop of the first object
So what is 'keyof'?
Now TypeScript can catch the error:

[TypeScript] Understand lookup types in TypeScript的更多相关文章
- [TypeScript] Query Properties with keyof and Lookup Types in TypeScript
The keyof operator produces a union type of all known, public property names of a given type. You ca ...
- [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] 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 Interface vs Types All In One
TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...
- 深入浅出TypeScript(2)- 用TypeScript创建web项目
前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...
- [TypeScript] Transform Existing Types Using Mapped Types in TypeScript
Mapped types are a powerful and unique feature of TypeScript's type system. They allow you to create ...
- [TypeScript] Model Alternatives with Discriminated Union Types in TypeScript
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of a ...
- [TypeScript] Using Assertion to Convert Types in TypeScript
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the c ...
- [TypeScript] Distinguishing between types of Strings in TypeScript
In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how T ...
随机推荐
- DOM和SAX是应用中操纵XML文档的差别
查看原文:http://www.ibloger.net/article/205.html DOM和SAX是应用中操纵XML文档的两种主要API.它们分别解释例如以下: DOM.即Do ...
- 构建自己的AngularJS - 作用域和Digest(一)
作用域 第一章 作用域和Digest(一) Angular作用域是简单javascript对象,因此你能够像对其它对象一样加入属性.然而,他们也有一些额外的功能:用于观測数据结构的变化.这样的观察能力 ...
- activity-启动动画的设定(下面弹出出现,弹入下面消失)
1.今天为了把一个activity以dialog的形式显示,而且实现从开始的时候从底部往上弹出,结束的时候,从上往下消失,做了如下的工作. 1)如果把一个activity以dialog的形式显示? 这 ...
- android图片特效处理之怀旧效果
图片特效处理系列将介绍图片的像素点的特效处理,这些物资注重的是原理.也就是说只要你知道这些算法不管是C++,VB,C#,Java都可以做出相同的特效.下面将介绍图片怀旧效果的算法.算法如下: 上面公式 ...
- HDU——T 1576 A/B
http://acm.hdu.edu.cn/showproblem.php?pid=1576 Time Limit: 1000/1000 MS (Java/Others) Memory Limi ...
- node中间层
node中间层 一.总结 1.node中间层作用:前端也是mvc,NodeJS之后,前端可以更加专注于视图层,而让更多的数据逻辑放在Node层处理 2.node中间层作用:当发现所有请求量太多应付不过 ...
- css中的!important作用
css中的!important作用 一.总结 1.!important:是hack, 2.!important作用:让浏览器首选执行这个语句,当对同一个对象设置了多个同类型的属性的时候,首选执行这一个 ...
- Mongodb总结5-通过装饰模式,用Mongodb解决Hbase的不稳定问题
最近继续学习Mongodb的根本原因,是为了解决今天的问题.项目中用到了Hbase,生产环境服务器用了3台,但是不够稳定,每2天左右,就连不上了.重启就好了,当然,这是一个历史遗留问题.我在想,是不是 ...
- 原生js大总结十
91.ajax的优点 a.提高运行效率 b.提高用户体验,让多件事情同时发生 c.在不刷新页面的情况下可以对局部数据进行加载和刷新 92.ajax请求的流程 1.创建通 ...
- Mysql学习总结(14)——Mysql主从复制配置
mysql主从复制 怎么安装mysql数据库,这里不说了,只说它的主从复制,步骤如下: 1.主从服务器分别作以下操作: 1.1.版本一致 1.2.初始化表,并在后台启动mysql 1.3. ...