Reputation: 8477
In JavaScript I can do:
window.eval(userInput)
in a client-side .js
file without any issue.
But in TypeScript, window.eval()
does not exist. I get the error:
property eval does not exist on type window
How can I get around this issue?
The reason I want to use eval()
is to execute some user created code. The eval
call must be done on a global scope because the user code relies on some other code that I have already previously loaded with <script>
tags.
Upvotes: 5
Views: 2400
Reputation: 612
There are a couple of ways to do this.
Use type assertion (Type unsafe, but quick and easy):
(window as any).eval("1 + 1");
Or you can modify the window declaration as described in this issue (Type safe)
Upvotes: 5