[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 ...
随机推荐
- [Python] String Formatting
One particularly useful string method is format. The format method is used to construct strings by i ...
- [React] Create a queue of Ajax requests with redux-observable and group the results.
With redux-observable, we have the power of RxJS at our disposal - this means tasks that would other ...
- DOM和SAX是应用中操纵XML文档的差别
查看原文:http://www.ibloger.net/article/205.html DOM和SAX是应用中操纵XML文档的两种主要API.它们分别解释例如以下: DOM.即Do ...
- 使用TCP协议的NAT穿透技术 (转载)
其实很早我就已经实现了使用TCP协议穿透NAT了,但是苦于一直没有时间,所以没有写出来,现在终于放假有一点空闲,于是写出来共享之. 一直以来,说起NAT穿透,很多人都会被告知使用UDP打孔这个技术,基 ...
- Hibernate之关于多对多单向关联映射
[Hibernate]之关于多对多单向关联映射 老师和学生,最典型的多对多关联, Teacher和Student.所谓单向意思就是说.老师知道自己的教的是哪些学生而学生不知道是哪些老师教. 也能够这么 ...
- 2.3 Streams API 官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 2.3 Streams API 2.3 Streams API 在0..0增加了一个 ...
- gdbserver 使用方法
1.分别编译出在宿主机运行的交叉调试器arm-linux-gdb和在目标板上运行的gdbserver: 2.在目标板开启gdbserver#gdbserver 宿主机ip:任意指定端口号 ./待调试 ...
- 最简单的实体手机测试移动端前端Vue Cli3搭建网站的方法
手机和PC同用一个路由的情况下,直接在手机的浏览器上输入Ip: 192.168.1.100:8080 就能看到了. 其中192.168.1.100是PC的IP.不同的自己改下就好. 就这么简单.啥都不 ...
- javaScript实现选中文字提示新浪微博分享的效果
<!DOCTYPE html> <html xmlns:wb="http://open.weibo.com/wb"> <head> <me ...
- 40.lombok在IntelliJ IDEA下的使用
转自:https://www.cnblogs.com/yjmyzz/p/lombok-with-intellij-idea.html lombok是一款可以精减java代码.提升开发人员生产效率的辅助 ...