本文へスキップ
Skillpedia
一般的なプログラミング75k · #2951

no-unchecked-indexed-access

できること

このスキルは、TypeScriptの安全オプションnoUncheckedIndexedAccessが有効か確認し、添字で配列の要素を確認せずに読むコードを修正します。実行時のundefinedエラーを減らしたい開発者向けです。

使う場面

  • ✓プロジェクトの設定に安全オプションが不足していないか確認するようエージェントに頼むとき。
  • ✓TypeScriptが警告しなかったundefinedの値でアプリが停止するとき。
  • ✓配列から位置で要素を読み取るデータ処理のコードを確認しているとき。

スキルのリンク

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-unchecked-indexed-access/SKILL.md

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-unchecked-indexed-access/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に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は配列の要素が常にあるものとして扱います。

多くの新しいエラーが出ますか?

出ることもあり、このスキルは、オプショナルチェーンとnullish合体演算子でほとんどを適切に扱えると説明しています。

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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル