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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. TypeScript Interface vs Types All In One

    TypeScript Interface vs Types All In One TypeScript https://www.typescriptlang.org/docs/handbook/adv ...

  5. 深入浅出TypeScript(2)- 用TypeScript创建web项目

    前言 在第一篇中,我们简单介绍了TypeScript的一些简单语法,那么如果我们只是简单使用TypeScript开发一个web项目,应该做哪些准备?接下来我们就结合TypeScript和Webpack ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Java开发者最经常使用19个Linux命令

    1.查找文件 find / -name filename.txt 依据名称查找/文件夹下的filename.txt文件. 2.查看一个程序是否执行 ps –ef|grep tomcat 查看全部有关t ...

  2. js18--继承方式

    方式1:子类.prototype = 父类对象 Boy.prototype = new Person(); Sub.prototype = new Sup('张三');   //可以传参数也可以不传 ...

  3. layoutParams-动态更改某个控件的margin

    其实它的方法也非常的简单,如下 LinearLayout.LayoutParams layoutParams = (LayoutParams) bt1.getLayoutParams(); int a ...

  4. IO流学习笔记

    1.File类 文件和目录路径名的抽象表示形式. 4种构造方法 File(File parent, String child) File(File parent, String child) File ...

  5. golang 建临时文件目录以及删除

    package main import ( "fmt" "os" "path/filepath" "strings" ) ...

  6. BZOJ5137: [Usaco2017 Dec]Standing Out from the Herd(广义后缀自动机,Parent树)

    Description Just like humans, cows often appreciate feeling they are unique in some way. Since Farme ...

  7. 洛谷 P1287 盒子与球

    P1287 盒子与球 题目描述 现有r个互不相同的盒子和n个互不相同的球,要将这n个球放入r个盒子中,且不允许有空盒子.问有多少种方法? 例如:有2个不同的盒子(分别编为1号和2号)和3个不同的球(分 ...

  8. hysbz 2243 染色(树链剖分)

    题目链接:hysbz 2243 染色 题目大意:略. 解题思路:树链剖分+线段树的区间合并,可是区间合并比較简单,节点仅仅要记录左右端点的颜色就可以. #include <cstdio> ...

  9. Gmail 收信的一些规则

    Gmail 收信的一些规则 用 apache+php+MDaemon 调试 mail2www 时,发往gmail的邮件失败, 提示: Our system detected an illegal at ...

  10. 27. Spring Boot 部署与服务配置

    转自“https://www.cnblogs.com/zhchoutai/p/7127598.html” Spring Boot 其默认是集成web容器的,启动方式由像普通Java程序一样,main函 ...