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

时间: 2024-11-17 02:02:59

[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 can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system. interface Todo { id: nu

[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 to more accurately type methods such as Object.create. Don't confuse it with the Object type or {}, the empty object type, though! Type 'Object' gives

[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 can combine literal types with union types to model a finite set of valid values for a variable. In this lesson, we explore the all kinds of literal t

[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 compiler with Typescript type assertion. We have a SuperHero and a BadGuy. Let's make a function that saves the day if the function's argument is a Sup

三、eggjs学习记录 - typescript声明放到types里面

如果项目需要做为一个模块被其它项目引用到,并用其它项目是用typescript写的, 这就需要兼容typescript, 需要把要导出的变量或者模块在types 目录里声明.对应的目录(文件名称)结构与真正的逻辑代码一致,只不过文件后缀改成.d.ts 就可以,不会被外部直接导入的文件,不需要写声明文件. //比如目录结构为: /** project/index.js, project/demo.js project/other/other.js */ //则可以增加声明文件: /** eggs/

[TypeScript] Using Interfaces to Describe Types in TypeScript

It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch errors at compile time or in an IDE. In this lesson we’ll learn how to describe a type shape with Typescript interfaces. Using interface to describe a

[TypeScript] Distinguishing between types of Strings in TypeScript

In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how Typescript catches string related errors at compile time by assigning a string literal as a type. type whiteList = "DOG" | "CAT" | "

[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

[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 they’re invoked. Using TypeScript’s function overloads, you can create an overload for each allowed combination of parameter and return types. This way,