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. 洛谷 P2105 K皇后

    P2105 K皇后 题目描述 小Z最近捡到了一个棋盘,他想在棋盘上摆放K个皇后.他想知道在他摆完这K个皇后之后,棋盘上还有多少了格子是不会被攻击到的. (Ps:一个皇后会攻击到这个皇后所在的那一行,那 ...

  2. Android——解决port占用问题导致的模拟器无法识别

    遇到一个问题:昨天模拟器工作还正常,今天eclipse就识别不了了.后来发现是360手机助手占用了5555port造成的,我就纳闷了,平时这个也不是自己主动启动.今天就启动了.废话不多说,就几个步骤就 ...

  3. 徒弟们对话,遇到sb领导,离职吧

     由于涉及私密,所以qq号做了干扰.见谅. 非常多人以为在公司,你优化了几十个sql老板就给你涨工资了.或者你bbed搞点特殊恢复就涨工资了. 或者解决某些棘手问题就涨工资了. 那是不正确的. 遇 ...

  4. 13.AxisUtil

    1. package com.glodon.gspm.adapter.plugin.common; import lombok.SneakyThrows; import org.apache.axis ...

  5. 洛谷 P4779【模板】单源最短路径(标准版)

    洛谷 P4779[模板]单源最短路径(标准版) 题目背景 2018 年 7 月 19 日,某位同学在 NOI Day 1 T1 归程 一题里非常熟练地使用了一个广为人知的算法求最短路. 然后呢? 10 ...

  6. 洛谷 P1054 等价表达式

    洛谷 P1054 等价表达式 题目描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代数表达式,然后列出了若干选项,每个选项也是一个代数表达式, ...

  7. CORS原理

    http://blog.csdn.net/renfufei/article/details/51675148 https://html.spec.whatwg.org/multipage/infras ...

  8. 洛谷——P1816 忠诚

    https://www.luogu.org/problem/show?pid=1816#sub 题目描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天记 ...

  9. C语言库函数,头文件

    参看:https://zhidao.baidu.com/question/328173842.html 该文件包含了的C语言标准库函数的定义 stdlib.h里面定义了五种类型.一些宏和通用工具函数. ...

  10. scrapy--介绍

    ​ Scrapy一个开源和协作的框架,其最初是为了页面抓取所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可用于如数据挖掘.监测和自动化测试等领 ...