跳到正文
Skillpedia
通用编程75k · #3066

type-coercion

它能做什么

这项技能检查 JavaScript 代码中可能产生意外结果的比较和类型转换。适用于希望减少隐藏错误的开发者。

适用场景

  • ✓当 JavaScript 代码使用 == 或 != 而不是严格比较的时候。
  • ✓当文字和数字以混合的方式相加的时候。
  • ✓当代码审查要求脚本中的类型处理更清晰的时候。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-coercion/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-coercion/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我的 JavaScript 有时会错误地比较数值。请帮我找出宽松的比较,并改为严格比较。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会找出宽松的相等检查和有风险的混合类型运算,并把它们替换为严格比较和明确的类型转换。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Avoid implicit type coercion

JavaScript's implicit type coercion produces results that look like bugs to anyone who doesn't know the exact coercion rules. == comparisons with mixed types silently convert values in ways that fail to catch genuine errors. Explicit conversions make code intent clear and catch real type mismatches at the point where they occur.

Quick Reference

  • Always use === and !== instead of == and !=
  • Convert types explicitly with Number(), String(), Boolean(), parseInt()
  • Be careful with + operator — it concatenates strings but adds numbers
  • Use Array.isArray() instead of typeof for array checks

Check

Find all uses of == and != (non-strict equality) in this file. Also look for implicit type conversions from + operator and other coercion traps.

Fix

Replace all == with === and != with !==. Add explicit type conversions where mixed types are intentionally compared.

Explain

Explain JavaScript's implicit type coercion rules, why == is problematic, and how to write safe type comparisons.

Code Review

Review scripts, client components, and browser execution paths related to Avoid implicit type coercion. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/type-coercion

常见问题

为什么 == 是个问题?

它在比较前会自动转换数值,这可能掩盖真正的错误,使结果难以预测。

检查数组更安全的方法是什么?

这项技能推荐使用 Array.isArray(),而不是 typeof 来检查数组。

应该如何转换类型?

使用明确的函数,例如 Number()、String()、Boolean() 或 parseInt()。

详情

作者的原始描述

Use when reviewing scripts, client components, bundles, or runtime behavior related to Avoid implicit type coercion. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#3066
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能