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

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。它会读取这个技能并按说明操作。

可直接粘贴的消息

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

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能