Beginner's TypeScript Section 18 exercises

Selectively Construct Types from Other Types

We've looked at inheriting and combining types.

But what if we want to be more selective?


Given this User interface, create a new object type that only includes the firstName and lastName properties:

interface User {
id: string;
firstName: string;

Loading exercise


0:00 We're continuing our Odyssey here of creating types from other types. It's really, really useful in TypeScript to be able to extract bits of a type and then create new ones out of it. Here, we've got our user type. What we want to do is create a new type here which is just the firstName and lastName properties of the user type.

0:23 How do we create an object type which is just those? We've got our test down here which is saying we want it to be firstName, lastName. The thing I want you to google in the TypeScript doc is utility types. There are various built-in utilities in TypeScript that I want you to see if you can have a look at. See if you can piece them together to work out how to create this MyType here.