Chuyển đến nội dung
Skillpedia
Lập trình chung75k · #2951

no-unchecked-indexed-access

Skill này làm gì

Skill này kiểm tra xem tùy chọn an toàn noUncheckedIndexedAccess của TypeScript đã bật chưa, rồi sửa mã đọc phần tử mảng theo chỉ số mà không kiểm tra. Skill dành cho lập trình viên muốn ít lỗi undefined lúc chạy hơn.

Khi nào nên dùng

  • ✓Khi bạn yêu cầu agent kiểm tra cài đặt TypeScript của dự án để tìm các tùy chọn an toàn còn thiếu.
  • ✓Khi ứng dụng sập vì giá trị undefined mà TypeScript không cảnh báo.
  • ✓Khi bạn rà soát mã xử lý dữ liệu đọc phần tử mảng theo vị trí.

Liên kết skill

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

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/no-unchecked-indexed-access/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Ứng dụng của tôi đôi khi sập với lỗi undefined. Bạn có thể bật thiết lập TypeScript bắt những lỗi này và sửa các đoạn mã nó đánh dấu không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Agent kiểm tra tệp tsconfig.json và bật tùy chọn nếu còn thiếu. Sau đó agent cập nhật các lần truy cập mảng mà TypeScript giờ đánh dấu, bằng cách thêm kiểm tra hoặc optional chaining để mã xử lý được phần tử bị thiếu.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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

Câu hỏi thường gặp

Vì sao hành vi mặc định của TypeScript ở đây lại rủi ro?

Mặc định, TypeScript coi phần tử mảng luôn tồn tại, ngay cả khi mảng rỗng hoặc chỉ số nằm ngoài phạm vi.

Có sinh ra nhiều lỗi mới không?

Có thể. Skill nói optional chaining và toán tử nullish coalescing xử lý hầu hết các trường hợp này một cách gọn gàng.

Còn Array.at() thì sao?

Skill giải thích cách Array.at() tương tác với kiểm tra này, để agent chọn đúng mẫu viết.

Chi tiết

Mô tả gốc của tác giả

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

Độ phổ biến

Sao GitHub
75k
Vị trí xếp hạng
#2951
Giấy phép
MIT
Cập nhật
31/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự