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.
- State has an error that says: State is defined but never used.
- 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?