Transcript
00:00 Alright. Let's jump in. So first, we're gonna make a config interface. Interface config. There we go.
00:05 So, this has an app name, and I can create, an app. All of that works just fine. And then before I go into, like, a cross module interface stuff, let's go ahead and make an interface, config that includes the theme light and dark. And we can do another interface config with the max connection. So then if I'm whoop.
00:28 Then if I make a config, I can have all of those properties in there and if I miss any of them, then I'm gonna get a warning. So this is something you cannot do with types. You cannot have interface interface interface or or type type type. If I were to switch this to type config app name type config that, already, I'm getting errors because you cannot have a duplicate identifier called config. That's just not allowed, with types.
00:53 But this is something you can do with interfaces. Now why would you do this? The question is like, why don't I just move that up there and then I'll move that one right there and now we're good. Right? Excellent question.
01:05 The where this comes in handy is when you're gonna be going across modules and and extending that or augmenting that interface. So, if you want to be able to do that, then you need to declare your, your interface on the global. And here, I don't know why it's not working. Let's take a look. Oh, right.
01:28 We hold on. Yeah. We need to have an export, because our script is being interpreted differently. So normally, you're not gonna have to worry about this, but in our little environment here, I had to add this export thing here. Oh, let's get rid of that, get thing.
01:43 There we go. And, yeah. Great. So so far, we have our interface called config. And now we've put it on the global name space.
01:51 So it's available to all scripts in the application. We can't put a theme on here. If I take that off, then we also can't put max connections on there. So to fix that, we want to augment the, the config in another module. So this is like some sort of plug in system that we're building or something.
02:08 We want to enable users of our plug in system to be able to augment our configuration. So I'm gonna do this import config augment and we'll dive into that file here. It's right next to our our index file. And we're going to use declare global, and here we gotta export, that to make this a module. We'll declare global interface config theme.
02:32 So we're looking in the global name space for the interface called config. And if it doesn't exist, we'll create it. But if it does exist, we're going to augment it with a theme. And we're also going to add, the max connections. Now, of course, we could do this separately.
02:50 We could put it together. We could do another declare global and put it there. Any of those are going to work just as well. And then we can come back over here. We've imported that augmentation.
03:00 And so now, our config is valid and it will totally work. And, of course, we can make our get theme and return that theme and export that theme. But the point here is that when you've got all the right things aligned, you've put your interface on the global namespace, or you can do this with module namespaces too. But you put it on the global namespace and, you then access it on the global namespace and and augment it in this way, you can change the interfaces that you didn't define. They're in library code somewhere else and you wanna change it.
03:37 So as long as they expose that on a module name space or a global name space you have access to, then you can make augmentations to it. Really, really powerful feature. And, yeah, it makes TypeScript pretty powerful. But I personally don't find myself doing I certainly don't find myself creating interfaces that I'm going to be augmenting every day. I don't even create interfaces every day.
04:02 And I don't augment other interfaces every day either. It's just not something that I do on a day to day. So in general, I recommend, just using types, and things go a lot more smoothly. But, yeah. Hopefully, that at least gives you some idea of what declaration merging is all about.
04:22 And let's, let's just call it a day. Good job on that one.
