After working with a TypeScript file containing the following code:
import { functionTest } from './function_test'
function runnerFunctionTest() {
console.log("Test");
}
export class Runner {
run(source : string) {
eval(source);
}
}
I encountered an issue where calling
run("runnerFunctionTest();")
would work fine, but calling run("functionTest();")
would result in an error stating that functionTest
is undefined.
How can this problem be resolved?
One attempted solution involved replacing the code within run
with
new Function('functionTest', source).(functionTest);
. While this worked, manually adding each imported function like this is not ideal, especially as the number of functions continues to grow over time.
It should also be noted that using eval
raises security concerns, but it seems to be necessary in order to execute user-generated JavaScript code within the browser.