Modules Scripts and Declaration Files 7 exercises

Type Variables Declared Elsewhere

Consider a variable called state that is resolved by DEBUG.getState():

const state = DEBUG.getState(); // red squiggly line under DEBUG
type test = Expect<Equal<typeof state, { id: string }>>;

Here, DEBUG acts like a global variable. In our hypothetical project, DEBUG is only

Loading exercise


00:00 We're going to move away from declaration files for a little bit, but we're going to need something called the declare keyword to fix this issue. And we've not covered this before, but they're very related to declaration files. So I think it makes sense to teach them together.

00:14 We have here a function called state, or rather a const called state, and it's being resolved by debug.getState. Debug is kind of like a global variable. But in fact, in our project, let's say in our imaginary like 1000 line project, we only have a reference debug in this particular file.

00:34 And it's a global that's being put into the global scope by let's say a dev tool or something. So all we want to do is within this file, we want to say, there is a const called debug, and we don't actually want to provide any implementation, we just want to provide a type.

00:51 And we're expecting the type of states to be instead of any that we're getting here, because we can't resolve it because of the error, we're expecting it to be an object with an ID of string.

01:00 So you're going to need to use the declare keyword and possibly declare const as well, to basically say that debug inside this module is available without needing to provide any implementation. Good luck. Interesting problem.