Modules Scripts and Declaration Files 7 exercises

Global Scope Declarations in TypeScript

The declare const keyword has set the DEBUG variable to be available in the local scope of index.ts:

// index.ts
declare const DEBUG: {
getState(): { id: string };
const state = DEBUG.getState();
type test = Expect<Equal<typeof state, { id: string }>>;

This means that wh

Loading exercise


00:00 Having just done the previous exercise where you declared a const debug and it just worked locally, you might be thinking, how can I get this declaration out into the global scope? Because this declare const is pretty useful, but if we have another file where we also need that debug,

00:17 it means that it's not being, can't reference it there unless we literally manually copy it over. So your job is to work out how to do that. There's one solution that you'll be able to find with all the information that you have already, which is declaration files and knowing how to

00:33 declare a declaration file as a module or as a script. So that's one solution, but there's another solution using a, what's called a declare global, which is a new piece of syntax, which we haven't quite seen before, but that's very similar in spirit to declare const debug here and will

00:50 means you don't need to declare the declaration file. Good luck. I think you've got everything. Yeah, I think so.