type-coercion
它能做什么
这项技能检查 JavaScript 代码中可能产生意外结果的比较和类型转换。适用于希望减少隐藏错误的开发者。
适用场景
- ✓当 JavaScript 代码使用 == 或 != 而不是严格比较的时候。
- ✓当文字和数字以混合的方式相加的时候。
- ✓当代码审查要求脚本中的类型处理更清晰的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/type-coercion/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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.
热度
- GitHub 星标
- 75k
- 排名
- #3066
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。