I have a module with several functions defined:
export function setNodeCount(count: i32): void {
nodeCount = count;
}
export function getNodeCount(): i32 {
return nodeCount;
}
These functions are imported into another TypeScript file using the following syntax:
import * as force from '../force';
I want to be able to mock or interchange the force
object with a variable that is in global scope.
To achieve this, I am creating an interface for the exported functions in this module:
interface Force {
setNodeCount: (number) => void;
getNodeCount: () => number;
}
This interface is then implemented either by the imported module or the global object:
const importImpl: Force = {
setNodeCount: force.setNodeCount,
getNodeCount: force.getNodeCount
}
const globalImpl: Force = {
getNodeCount: globalObject.getNodeCount,
setNodeCount: globalObject.setNodeCount
};
At runtime, I can choose between these two implementations.
Is there a simpler way to define the interface for an imported module and dynamically replace it?