JavaScript 的类型检查器?

发布于 2024-10-16 12:17:59 字数 276 浏览 0 评论 0原文

有谁知道是否有一个分析 JavaScript 代码和检测类型错误的好工具?我知道 JavaScript 本身是弱类型和动态类型的,但如果我能有一个程序来验证我的所有字段访问是否合理并且我不会尝试将数字视为字符串,那就太好了。我知道 JavaScript 中有一些有效的用例,其中添加或删除字段或在不同类型之间进行转换是有效且符合预期的,但有些错误非常明显,似乎可以在脚本开始运行之前捕获它们。

我听说过 JSLint,但我的理解是它主要是一个样式工具而不是语义分析器。如果我错了,那么只要告诉我,这就是这个问题的有效答案。

Does anyone know if there's a good tool for analyzing JavaScript code and detecting type errors? I know that JavaScript itself is weakly and dynamically typed, but it would be really nice if I could have a program that would verify that all my field accesses are sensible and that I don't try treating a number like a string, for example. I'm aware that there are valid use cases in JavaScript where adding or removing fields or converting between different types is valid and expected, but some errors are so blatant it seems like they could be caught before the script started running.

I've heard of JSLint, but my understanding is that it's mostly a style tool rather than a semantic analyzer. If I'm wrong about this, then just telling me so would be a valid answer to this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦归所梦 2024-10-23 12:17:59

如果您遵循 Google 闭包编译器使用 jsDoc 注释注释函数的建议,则会进行一些类型检查。请参阅 http://code.google.com/closure/compiler/文档/js-for-compiler.html

Google closure compiler does some type checking if you follow their recommendations for annotating your functions with jsDoc comments. See http://code.google.com/closure/compiler/docs/js-for-compiler.html

不…忘初心 2024-10-23 12:17:59

另一种选择 - https://github.com/yarax/typelint

这是一个 ESLint 插件,它可以基于 JSDoc 注释的类型检查。

此外,它还支持自己的扩展,以使用 Redux 状态或 Swagger 模式(基本上是任何 JSON 模式)进行类型检查。所以你不需要自己去描述复杂的复合类型。

One another option - https://github.com/yarax/typelint

It's an ESLint plugin, that does type checking based on JSDoc annotations.

In addition it supports its own extension to use Redux state or Swagger schemas (basically any JSON schema) for type checking. So you don't need to describe complex composite types by yourself.

乖乖兔^ω^ 2024-10-23 12:17:59

由于 JavaScript 的动态特性,不可能使用任何自动化工具充满信心地进行静态分析。不过,Jetbrain 的 WebStorm 及其其他支持 JavaScript 的 IDE 是一个表现出色的工具。

It's not possible to do static-analysis with confidence using any automated tool owing to the dynamic nature of JavaScript. However, one tool that does a great job is Jetbrain's WebStorm and their other IDEs that have JavaScript support.

⊕婉儿 2024-10-23 12:17:59

下面是一个关于如何在 JavaScript 中进行类型检查的基本示例:

const Int = (val) => {
    if (Number.isInteger(val)) {
        return val;
    }else {
        throw new TypeError(`Argument value ${val} is not integer`);
    }
    return 0; //Unreachable: for static analysis
};

const sum = function(_, numA = Int(_[0]), numB = Int(_[1])) {
    return `Sum is ${numA + numB}`;
};

console.log(sum([1, 2])); //correct
console.log(sum([1, "2"])); //incorrect

Below is a basic example on how to do type checking in JavaScript:

const Int = (val) => {
    if (Number.isInteger(val)) {
        return val;
    }else {
        throw new TypeError(`Argument value ${val} is not integer`);
    }
    return 0; //Unreachable: for static analysis
};

const sum = function(_, numA = Int(_[0]), numB = Int(_[1])) {
    return `Sum is ${numA + numB}`;
};

console.log(sum([1, 2])); //correct
console.log(sum([1, "2"])); //incorrect

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文