Lookup Types

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Professional TypeScript Masterclass Lessons

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11

Lookup Types

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Motivation

Can use used to collect the type of some property e.g.

export type SubmitRequest = {
transactionId: string,
personal: {
title: string,
driverFirstName: string,
driverMiddleName: string,
driverLastName: string,
email: string,
phone: string,
previousAliases: {
firstName: string,
middleName: string,
lastName: string,
}[],
gender: string,
dob: string,
birthCountry: string,
},
payment: {
creditCardToken: string,
}
};

// Lookup type
type PaymentRequest = SubmitRequest['payment'];

export function getPayment(): PaymentRequest {
return {
creditCardTokens: '124q234n12l!@#$3412n34!@#$'
}
}

Nesting

You can nest further and even use array indexes e.g.:

export type SubmitRequest = {
transactionId: string,
personal: {
title: string,
driverFirstName: string,
driverMiddleName: string,
driverLastName: string,
email: string,
phone: string,
previousAliases: {
firstName: string,
middleName: string,
lastName: string,
}[],
gender: string,
dob: string,
birthCountry: string,
},
payment: {
creditCardToken: string,
}
};

// Lookup types chain
type PreviousAliasRequest = SubmitRequest['personal']['previousAliases'][number];
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

I once worked on an application where the backend needed a bunch of information specified by a type called submit request similar to what is shown over here. Then different portions of the UI would collect portions of this request and send it back to the backend. For example, a portion of the UI was dedicated to the purpose of collecting the credit card token. Now, in order to ensure consistency between the return type of the get payment method and the payment portion of the submit request, we could extract that portion out into its own name type for example, payment request,

00:34

and then export that from the common library and then use it in our UI application. But this leads to two problems. First, we need to start asking the backend team to make modifications to their type definitions, and additionally, we will probably start asking that for other portions of the submit request as well. A second issue with this approach is that now for someone looking at just the submit request, they would see various members pointing to different types and it would not be immediately clear to them why these types need to be split out. These additional types are essentially type noise in the common library.

01:07

The solution to both of these problems is provided by typescripts lookup types. So we get rid of the return type annotation, get rid of this extra type within our common library duty, and then revert submit request to all be in line. Now, using typescripts lookup types, we can look up the type of the payment member from submit request on the fly. The syntax for looking up the type of a particular member is similar to the syntax of looking up a member from a JavaScript object. Here we are saying that the return type of the get payment method should be the same as the type of the payment member of submit request.

01:42

This return type annotation ensures that we return the correct type and if you make any silly typos, for example, add an extra S, you can see that TypeScript points it out as a mistake. Now we have used this lookup type in line, but if you find yourself using the same type again and again, you are still free to create a type alias using a lookup type. This is still better than our previous solution because a submit request is still one cohesive type. And additionally, this particular utility type can live in our UI project and not in the common library.

02:15

Now, lookup types can also be used to look up the type of the items in an array. Within our type submit request, we have a member personal that has a member previous aliases, which is an array of items as indicated by these square brackets. So a previous alias is something that has three members, first name, middle name, and last name. Let's see how we can get that type. So we jump back to the UI portion of our code, and the first thing that we will do is that from submit request, we look up the member personal, and then from the type types of metric request personal,

02:49

we look up the member previous aliases as we saw before, this particular member is an array. So to get the type of the items in the array, all we need to do is to look up the first item by using an index zero. And now our previous alias request is exactly what we wanted. That is a type with three members, first name, middle name, and last name.

Professional TypeScript Masterclass

Professional TypeScript Masterclass

1.Introduction
free
⏱️ 1:54
2.Setup
free
⏱️ 5:44
3.Primitive Types
free
⏱️ 1:42
4.Instance Types
free
⏱️ 1:52
5.Arrays And Tuples
free
⏱️ 1:38
6.Objects
free
⏱️ 1:33
7.const declarations
free
⏱️ 1:03
8.Function Types
free
⏱️ 1:57
9.Structural Typing
free
⏱️ 2:10
10.Classes in TypeScript
free
⏱️ 1:48
11.Target Compiler Option
free
⏱️ 2:37
12.Generics
⏱️ 3:02
13.Special Types any And unknown
⏱️ 2:00
14.JavaScript to TypeScript
⏱️ 1:32
15.Frontend Projects
⏱️ 3:49
16.Type Assertions
⏱️ 2:15
17.Type Casting
⏱️ 1:16
18.Modules
⏱️ 1:55
19.Type Declarations
⏱️ 4:25
20.Creating NPM packages
⏱️ 3:20
21.Async Await
⏱️ 3:05
22.Running in NodeJS
⏱️ 1:40
23.Lexical this
⏱️ 2:34
24.readonly Modifier
⏱️ 1:59
25.Union Types
⏱️ 2:57
26.Literal Types
⏱️ 2:58
27.Type Narrowing
⏱️ 4:19
28.Discriminated Unions
⏱️ 3:29
29.Class Parameter Properties
⏱️ 1:02
30.Strict Compiler Option
⏱️ 6:18
31.null vs undefined
⏱️ 4:19
32.Intersection Types
⏱️ 2:03
33.Optional Modifier
⏱️ 2:47
34.Non Null Assertion Operator
⏱️ 3:40
35.Interfaces
⏱️ 2:28
36.Interface Declaration Merging
⏱️ 1:01
37.Types vs Interfaces
⏱️ 2:16
38.never Type
⏱️ 3:00
39.implements Keyword
⏱️ 1:25
40.Definite Assignment Assertion
⏱️ 2:31
41.User Defined Type Guards
⏱️ 2:02
42.Assertion Functions
⏱️ 3:42
43.Function Overloading
⏱️ 4:15
44.Call Signatures
⏱️ 2:53
45.Abstract Classes
⏱️ 1:53
46.Index Signatures
⏱️ 3:08
47.Readonly Arrays and Tuples
⏱️ 2:58
48.Double Assertions
⏱️ 2:20
49.const Assertions
⏱️ 3:55
50.this Parameter
⏱️ 2:33
51.Generic Constraints
⏱️ 2:43
52.typeof Type Operator
⏱️ 2:12
53.Lookup Types
⏱️ 3:12
54.keyof Type Operator
⏱️ 3:55
55.Conditional Types
⏱️ 4:39
56.Contitional Types with Unions and never
⏱️ 3:32
57.infer Keyword and `ReturnType<T>`
⏱️ 3:47
58.Mapped Types
⏱️ 2:48
59.Mapped Type Modifiers
⏱️ 3:37
60.Template Literal Type
⏱️ 4:28
61.Partial<T>
⏱️ 1:27
62.Required<T>
⏱️ 1:36
63.Readonly<T>
⏱️ 1:34
64.Record<K, T>
⏱️ 4:05
65.Project References
⏱️ 4:18
66.undefined vs. optional
⏱️ 2:48
67.satisfies Operator
⏱️ 2:42
68.PropertyKey Type
⏱️ 0:57
69.ThisType<T>
⏱️ 4:11
70.Awaited<T>
⏱️ 4:12
71.String Manipulation Types
⏱️ 3:36
72.Mapped Types as Clauses
⏱️ 4:01
73.Union vs Intersection Mental Model
⏱️ 3:36
74.Enums are Bad
⏱️ 8:11