Token导航 LogoToken导航TokenDH.com
前端设计需要联网github未标认证来源可访问许可证需确认审计通过

iterate-objects-safely安全地迭代对象

Agent Skill

iterate-objects-safely 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

220

周安装

9

GitHub Stars

2

下载量

71
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:iterate-objects-safely(安全地迭代对象)
来源仓库:https://github.com/marius-townhouse/effective-typescript-skills
仓库路径:skills/iterate-objects-safely
安装命令:
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill iterate-objects-safely
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/marius-townhouse/effective-typescript-skills --skill iterate-objects-safely

简介

用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息。

  • 适合围绕仓库状态、代码变更或协作事项进行整理。
  • 可结合来源仓库和原始 README 核验具体用法。
  • 安装前建议确认权限范围和维护状态,以及是否会触发联网或文件读写。
  • 注意检查是否会触发命令执行或高风险操作。iterate-objects-safely 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Know How to Iterate Over Objects

Overview

Iterating over objects in TypeScript is surprisingly tricky. The for...in loop infers keys as string rather than the object's keys, leading to indexing errors. This happens because objects can have additional properties beyond their declared type (structural typing), and for...in includes inherited properties.

Understanding safe iteration patterns helps you avoid any types and type assertions while correctly handling object traversal.

When to Use This Skill

  • Iterating over object keys and values
  • for...in loops produce "Element implicitly has 'any' type" errors
  • Object.entries returns any value types
  • Need to handle both known and unknown object properties
  • Considering Map vs object for data storage

The Iron Rule

Use Object.entries for safe iteration over any object. Use for...in with keyof assertions only when you know the exact shape. Consider Map for guaranteed type safety.

Detection

Watch for these errors:

// ERROR: Element implicitly has 'any' type
for (const k in obj) {
  const v = obj[k];  // Error!
}

// ERROR: Type 'string' cannot be used to index type 'ABC'
function foo(abc: ABC) {
  for (const k in abc) {
    const v = abc[k];  // Error!
  }
}

The Problem

interface ABC {
  a: string;
  b: string;
  c: number;
}

function foo(abc: ABC) {
  for (const k in abc) {
    // k is string, not 'a' | 'b' | 'c'
    const v = abc[k];
    //     ^? any - TypeScript gives up
  }
}

// Why? Structural typing allows extra properties:
const x = { a: 'a', b: 'b', c: 2, d: new Date() };
foo(x);  // Valid! x has at least ABC's properties

// k could be 'd', which isn't in ABC

Safe Iteration with Object.entries

function foo(abc: ABC) {
  for (const [k, v] of Object.entries(abc)) {
    // k: string (honest about what it is)
    // v: any (honest about unknown values)
    console.log(k, v);
  }
}

Pros: Always safe, no type assertions needed Cons: Values are any, keys are string

Iterating with Known Keys

When you know exactly what keys exist:

const obj = { one: 'uno', two: 'dos', three: 'tres' };

// Explicitly list keys
const keys = ['one', 'two', 'three'] as const;
for (const k of keys) {
  // k: 'one' | 'two' | 'three'
  const v = obj[k];  // string - precise!
}

Type Assertion for Closed Objects

When you're sure about the object's shape:

const obj = { one: 'uno', two: 'dos', three: 'tres' };

for (const kStr in obj) {
  const k = kStr as keyof typeof obj;
  // k: 'one' | 'two' | 'three'
  const v = obj[k];  // OK
}

Warning: Only safe when you control object creation and know it has no extra properties.

Map: The Type-Safe Alternative

const m = new Map([
  ['one', 'uno'],
  ['two', 'dos'],
  ['three', 'tres'],
]);

for (const [k, v] of m) {
  // k: string (known type)
  // v: string (known type)
  console.log(k, v);
}

Pros: Guaranteed types, no prototype pollution Cons: Less convenient for JSON data, different API

Real-World Example

interface Config {
  host: string;
  port: number;
  ssl: boolean;
}

// Safe iteration with Object.entries
function printConfig(config: Config) {
  for (const [key, value] of Object.entries(config)) {
    console.log(`${key}: ${value}`);
  }
}

// Type-safe with known keys
function validateConfig(config: Config): string[] {
  const errors: string[] = [];
  const requiredKeys = ['host', 'port', 'ssl'] as const;

  for (const key of requiredKeys) {
    if (!(key in config)) {
      errors.push(`Missing: ${key}`);
    }
  }

  return errors;
}

Pressure Resistance Protocol

When object iteration causes type issues:

  1. Try Object.entries: Safest option, works with any object
  2. Consider Map: If you control the data structure
  3. List keys explicitly: When keys are known at compile time
  4. Use type assertions sparingly: Only when you're certain about object shape

Red Flags

Anti-PatternProblemSolution
obj[k] in for...ink is string, unsafe indexUse Object.entries
as keyof T on parametersObject might have extra keysObject.entries or Map
Ignoring inherited propertiesPrototype pollution riskObject.entries excludes these

Common Rationalizations

"I'll just use as any"

Reality: Object.entries gives you the same any but honestly. No need to bypass type safety.

"This object will never have extra properties"

Reality: TypeScript's structural typing means it could. Be explicit about your assumptions.

"Map is too verbose"

Reality: It's more verbose but also more type-safe. Worth it for critical code.

Quick Reference

ApproachKey TypeValue TypeSafety
for...instringany (error)Unsafe
Object.entriesstringanySafe
Known keys arraySpecificSpecificSafe
keyof assertionSpecificSpecificRisky
MapSpecificSpecificSafe

The Bottom Line

Object iteration is tricky due to structural typing and prototype pollution. Use Object.entries for safety, explicit key arrays for precision, or Map for guaranteed type safety.

Reference

  • Effective TypeScript, 2nd Edition by Dan Vanderkam
  • Item 60: Know How to Iterate Over Objects

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

35.88%
按下载量换算25

Claude

27.18%
按下载量换算19

Cursor

18.22%
按下载量换算13

Gemini CLI

8.86%
按下载量换算6

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills