Colocating Types for Global Interfaces
This exercise shows you some cool stuff you can do with
declare global in your application.
There are multiple problem files here!
Inside the Problem 1 File
0:00 In this exercise, I'm going to be showing you the cool things you can use declare global for in your application, not interfacing with external library code. Here we have a declare global where we have an interface dispatchable event. We have this login property on it with a username and password.
0:17 Down here we have -- this is inside the declare global as well, by the way, inside the ambient context -- we have a union of dispatchable events where we take K in key of dispatchable events. We basically grab the K, grab the key, the type here, and we turn it into a discriminated union.
0:35 You'd be able to learn about this syntax in the type transformations module. Now, what happens here is you end up with a union of dispatchable events. If I duplicate this, if I say, "Login 2," for instance, then we should be able to have a username 2 and a password 2 on here. That will be added to the union.
0:55 You can see the first member of the union here -- login. Then you have login 2 down here. This means that when you call dispatch event, you're going to get type, login 1 and login 2, shown up. Let me remove that briefly. What I want to be able to do is I want to be able to...from inside this file, I have this handler.
1:17 This handler is a reducer, let's say. It's taking in a union of dispatchable events, but logout is not on there. This logout, we should be able to be able to pass that into our handler. We should also be able to pass in "Update username," too. Except I want to be able to do it from within this file.
1:39 I want to say, "I have my handler co-located with the events that I expect. Can I do it from within this file?" You can. You'll need a declare global. You'll need to understand all of the stuff about ambient context and declaration merging that we've learned already. You should be able to figure it out from there. Good luck.