Skip to main content


Disallow the use of eval()-like methods.


该规则需要 类型信息 才能运行。

避免使用 eval() 被认为是一个好习惯。 这样做会涉及安全和性能问题,这就是为什么许多 linter 建议禁止 eval()。 然而,还有一些其他方法可以传递字符串并将其解释为具有类似问题的 JavaScript 代码。

英:It's considered a good practice to avoid using eval(). There are security and performance implications involved with doing so, which is why many linters recommend disallowing eval(). However, there are some other ways to pass a string and have it interpreted as JavaScript code that have similar concerns.

第一种是使用 setTimeout()setInterval()setImmediateexecScript()(仅限 Internet Explorer),所有这些都可以接受一串代码作为其第一个参数

英:The first is using setTimeout(), setInterval(), setImmediate or execScript() (Internet Explorer only), all of which can accept a string of code as their first argument

setTimeout('alert(`Hi!`);', 100);

或使用 new Function()

英:or using new Function()

const fn = new Function('a', 'b', 'return a + b');

这被认为是隐含的 eval(),因为传入了一串代码进行解释。 对于 setInterval()setImmediate()execScript() 也可以进行同样的操作。 所有这些都在全局作用域内解释 JavaScript 代码。

英:This is considered an implied eval() because a string of code is passed in to be interpreted. The same can be done with setInterval(), setImmediate() and execScript(). All interpret the JavaScript code in the global scope.

最佳实践是避免使用 new Function()execScript(),并始终使用函数作为 setTimeout()setInterval()setImmediate() 的第一个参数。

英:The best practice is to avoid using new Function() or execScript() and always use a function for the first argument of setTimeout(), setInterval() and setImmediate().


该规则旨在通过使用 new Function()setTimeout()setInterval()setImmediate()execScript() 来消除隐含的 eval()

英:This rule aims to eliminate implied eval() through the use of new Function(), setTimeout(), setInterval(), setImmediate() or execScript().

/* eslint @typescript-eslint/no-implied-eval: "error" */

setTimeout('alert(`Hi!`);', 100);

setInterval('alert(`Hi!`);', 100);



window.setTimeout('count = 5', 10);

window.setInterval('foo = bar', 10);

const fn = '() = {}';
setTimeout(fn, 100);

const fn = () => {
return 'x = 10';
setTimeout(fn(), 100);

const fn = new Function('a', 'b', 'return a + b');
Open in Playground


如果你的项目很少需要允许带有字符串参数的 new Function()setTimeout()setInterval()setImmediate()execScript(),那么你可以禁用此规则。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:If your project is a rare one that needs to allow new Function() or setTimeout(), setInterval(), setImmediate() and execScript() with string arguments, then you can disable this rule. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.


module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"no-implied-eval": "off",
"@typescript-eslint/no-implied-eval": "error"
在线运行试试这个规则 ↗


参见 eslint/no-implied-eval 选项


摘自 ❤️ ESLint 内核