Skip to content
← Back to rules

typescript/no-implied-eval Correctness

This rule is turned on by default when type-aware linting is enabled.
💭 This rule requires type information.

作用

此规则禁止使用类似 eval 的方法。

为什么这不好?

在 JavaScript 中避免使用 eval() 被认为是一种良好实践。这样做会带来安全和性能方面的影响,因此许多 linter 建议禁止使用 eval()。不过,还有一些其他方式可以传入字符串并将其解释为 JavaScript 代码,它们也有类似的风险。

示例

此规则的错误代码示例:

ts
setTimeout('alert("Hi!");', 100);

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

setImmediate('alert("Hi!")');

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

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

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

此规则的正确代码示例:

ts
setTimeout(() => {
  alert("Hi!");
}, 100);

setInterval(() => {
  alert("Hi!");
}, 100);

setImmediate(() => {
  alert("Hi!");
});

const fn = (a: number, b: number) => a + b;

如何使用

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/no-implied-eval": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/no-implied-eval": "error",
  },
});
bash
oxlint --type-aware --deny typescript/no-implied-eval

版本

此规则在 v1.12.0 中新增。

参考资料