0

I'm trying to write some unit tests for some legacy JavaScript code, and I want these unit tests to use TypeScript so i can have some sort of type safety. I pull this off by loading a global file before my tests run. It looks like this:

(global as any).setup = {};

(global as any).State = {
  variables: {},
};

This works fine, but I don't like how i'm using any there, and I'd like to use a real type instead. Here was my attempt to do so:

/* eslint-disable @typescript-eslint/no-explicit-any */
console.log("global");

(global as any).setup = {};

interface SugarcubeVariables {

}

interface SugarcubeState {
    variables: SugarcubeVariables;
}

State: SugarcubeState = {
  variables: {},
};

With this change, I get to errors that I don't know how to resolve.

  1. State has an error that says: State is defined but never used.
  2. SugarcubeState has an error that says: SugarcubeState is not defined

That 2nd one is more confusing to me, because I have an interface definition right above it.

If I put a const in front of State, the errors turn into a warning: State is assigned a value but never used.

In my test, I have a line like this: const State = (global as any).State; Before I modified anything above, this worked fine, but now that I've changed State in the global file, I don't know how to refer to it anymore.

I can't tell if I'm going about this completely wrong or I'm getting closer to solving the problem. How can I modify (global as any).State in the global file so that it's no longer using any, but it's still a global variable?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • Are you looking to type *a* global variable that's set outside of your Typescript code, or *the* global variable (e.g. window, global, self)? – Jared Smith Mar 20 '20 at 21:27
  • @JaredSmith the former. This variable is called State – Daniel Kaplan Mar 20 '20 at 21:28
  • @Mr.AF I read that but it doesn't seem relevant to me – Daniel Kaplan Mar 20 '20 at 21:30
  • On the linked dupe, make sure you go with the most-upvoted answer rather than the objectively awful accepted one. – Jared Smith Mar 20 '20 at 21:31
  • @JaredSmith I'm going to try out that answer and see if it works. I'm inferring that the Window/Global interfaces are special interfaces? – Daniel Kaplan Mar 20 '20 at 21:34
  • They may be, but you can modify any interface that way, [see here.](https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgGIHt3IN4ChkHIyYBcyIArgLYBG0uAvrrqJLIihlnocjXFDIBnMFFABzADSNmCdCBFFM-QWkzIAvDnyFi6MgGZpvFWQDkAC2BnpTIA) – Jared Smith Mar 20 '20 at 21:36
  • @JaredSmith Maybe this is worthy of a new question, I don't know, but following that advice I got Visual Studio code to stop complaining. On the other hand, it seems like Jasmine is unaware of that .d.ts file, and whenever I try to run my tests, it errors saying `Cannot find name 'SugarcubeState'.` – Daniel Kaplan Mar 20 '20 at 21:52
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210035/discussion-between-jared-smith-and-daniel-kaplan). – Jared Smith Mar 20 '20 at 22:30

0 Answers0