no-unchecked-indexed-access
它能做什么
这个技能检查 TypeScript 的安全选项 noUncheckedIndexedAccess 是否已开启,并修复那些按索引读取数组项却未检查的代码。它面向希望减少运行时 undefined 错误的开发者。
适用场景
- ✓你请智能体检查项目的 TypeScript 设置是否缺少安全选项的时候。
- ✓应用因为 TypeScript 未曾警告的 undefined 值而崩溃的时候。
- ✓你在检查按位置从数组中读取数据的处理代码的时候。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-unchecked-indexed-access/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我的应用有时会因为 undefined 错误而崩溃。请帮我开启能发现这类问题的 TypeScript 设置,并修复它标出的代码吗?”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会检查 tsconfig.json 文件,如果缺少该选项就将其开启。然后它会更新 TypeScript 现在标出的数组访问,加入检查或可选链,让代码能处理缺失的项。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Enable noUncheckedIndexedAccess to catch out-of-bounds array bugs
TypeScript's default behaviour types array[0] as T, pretending the access is always safe. In reality, the array might be empty or the index might be out of range, leading to silent undefined values that cause downstream crashes. Enabling noUncheckedIndexedAccess surfaces these potential failures as type errors at compile time, requiring developers to prove the access is safe before the code runs.
Quick Reference
- Without the flag, array[0] is typed as T even if the array is empty
- noUncheckedIndexedAccess changes array[n] to return T | undefined
- Forces an explicit check before using any array element accessed by index
- Optional chaining and nullish coalescing handle most false positives cleanly
Check
Check whether noUncheckedIndexedAccess is enabled in the project's tsconfig.json. If not, scan for array index accesses (arr[0], arr[i]) that could fail if the array is shorter than expected.
Fix
Enable noUncheckedIndexedAccess in tsconfig.json, then update any array index accesses that TypeScript now flags to include an explicit undefined check or use optional chaining before the accessed value is used.
Explain
Explain what noUncheckedIndexedAccess does, why the default TypeScript behaviour is unsound for array index access, and how optional chaining and Array.at() interact with the flag.
Code Review
Review all array index accesses in this file (arr[0], arr[i], object[key]). Flag any location where the accessed value is used without a preceding undefined check, even if noUncheckedIndexedAccess is not yet enabled in the project.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/javascript/no-unchecked-indexed-access
常见问题
为什么 TypeScript 的默认行为在这里有风险?
默认情况下,TypeScript 会认为数组项一定存在,即使数组为空或索引超出范围。
这会不会产生很多新错误?
可能会。这个技能说,可选链和空值合并运算符可以很干净地处理大部分情况。
Array.at() 怎么办?
这个技能说明了 Array.at() 与这项检查如何配合,以便智能体选择正确的写法。
详情
作者的原始描述
Use when auditing tsconfig.json for missing safety flags, reviewing code that uses array index access in data processing pipelines, or investigating runtime undefined errors that TypeScript did not catch.
热度
- GitHub 星标
- 75k
- 排名
- #2951
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。