Making TypeScript Stick

Penpal Types Exercise

Making TypeScript Stick

Check out a free preview of the full Making TypeScript Stick course

The "Penpal Types Exercise" Lesson is part of the full, Making TypeScript Stick course featured in this preview video. Here's what you'd learn in this lesson:

Students are instructed to create a utility type WrapForPenpal<T> that takes an object T with methods and emits a type with similar methods. Any non-promise return types become ”Promise-ified.”


Transcript from the "Penpal Types Exercise" Lesson

>> The last thing we're gonna do in this class is a final challenge. And this is actually, it's a typing problem that I had to solve in my journey to mastering TypeScript. So a couple years ago, I was involved in a lot of work around web workers and if you've never used these before, you can think of it as kind of a fully independent program that runs and you can talk to it through the post message API.

Similar to how you can have something I framed in a larger application and use post message to go back and forth between these things. So, the post message API is excellent it's one manifestation of the actor concurrency model, which is a effective way to make sure that things are never deadlocked, where A is waiting on B and B is waiting on A.

Effectively like these two programs, they each have mailboxes, they can send messages to each other, but they have no opinion on when those messages are read, can think of it that way. So it's a very good concurrency model. It's been around for a very long time. However, the way asynchronous stuff works in JavaScript now it's it's more common to use promises right?

Same reason like no Jas they used to have all this everything written in callback API's and now everything's promise based right? Like reading a file you get a promise back with the contents of the file. So I found this great library called PenPal, which lets you define methods in your worker and what you end up with is a very similar method on an object in your main application that has a similar type except the return value is kind of wrapped in a promise.

So you can see here this is what might be in the worker and I have this object with methods on it or functions on it. And add returns a number and then if I wanted my web worker to do this thing, I would create a connection with the web worker and get this child thing back.

And I could simply invoke add on this thing and I'd get back a promise that resolves to a number. So I can kinda of have a higher level way to talk with this thing that effectively is running in a separate thread very convenient. So let me show you the read me for this library and we're gonna implement a simplified version of this, but I just want you to kind of get the idea of how this is meant to be used.

So this is code that might exist in the parent window. And you can see here we've got this connect to child function. And here are your methods. Then we've got add, right? Just sums up numbers 1 and 2, and then it connects to this child and then child has a multiply and a divide.

Here they are, multiply and divide to their synchronous here, up here it's kind of feels like you're just calling this thing directly. The promise Is where the data is sent over there, it's run through the function, the results collected comes back through the posts channel, and then you get it back in your parent frame.

So we're not gonna worry about any of this connection aspect of things. I just want us to focus on how might we solve the interesting type information problem here. How do you take an object with some functions on it and create the type for a new object where anything that was not a promise is effectively wrapped in a promise.

That's what you're setting out to do here. So that's your task you're gonna create a type called rap for PenPal you're gonna pass in type of methods which will give you the interface like corresponding to this object. And we should see that asyncmethods.add should return a promise that resolves to a number, etc, etc.

Hopefully this makes sense. In this case, do not worry about making sure the code runs. We just want the type information to be what you're dealing with. So in the workshop project. You can see this async communicator folder. It's the final the three folders in this challenges area and there's a source folder in there and an index.ts.

So, let me get rid of this other stuff here. In this case, there is no test suite to run. Your tests are just for type information and there at the bottom here. So here you can see that like we've got a sink methods.add. And, right now this ends up being an any.

That's why we see two places where we see red squiggles. So by the time you're done, these things should go away. These are things that should error, and currently they are not erroring. That's why type scripts objecting here. So these are negative test cases. So your job is, make this work, right replace this anything with something that gives us equivalent function types with equivalent argument lists.

And then anything that's not a promise should be kind of wrapped in a promise. And, I believe as an extra challenge consider that you might already have something here that's asynchronous. So if you want to a stretch goal here, don't wrap promises in double promises, right? So if we have like.

Do a sync thing that returned a promise that resolves to a number something like this right? There's no need to make this a promise that resolves to a promise that resolves to a number you can just make it like leave it alone effectively. A stretch goal here because this will require a little bit extra work to handle this case you'll have to detect whether you're in that situation and either do something or not do something.

So you should end up really working on line four up here only do this if you can make the rest of it work and you want to get fancy and then wrap up the course

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now