avoid-numeric-index

Replace numeric index signatures with Array, tuple, ArrayLike, or Map in TypeScript types.

Updated Jul 17, 2017
One-click install
npx skills add https://github.com/luyi985/lyi-bash --skill avoid-numeric-index
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: avoid-numeric-index
Source: https://github.com/luyi985/lyi-bash/tree/main/ai/skills/avoid-numeric-index
Command: npx skills add https://github.com/luyi985/lyi-bash --skill avoid-numeric-index

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

JavaScript's runtime keys are always strings, while TypeScript supports number-index signatures as a compile-time fiction. This Skill guides developers to avoid { [n: number]: T } and choose Array<T>, tuple, ArrayLike<T>, or Map<number, T> when modeling collections.

Core Features & Use Cases

  • Understand the runtime behavior of object keys and why numeric indices are not real indices at runtime.
  • Learn when to prefer arrays, tuples, ArrayLike<T>, or Map<number, T> over numeric index signatures in type definitions.
  • Apply guidance to common scenarios like modeling sequences, fixed-length data, and keyed collections that rely on numeric keys.

Quick Start

Replace any numeric index signature with an Array or Map in your TypeScript types and verify runtime key behavior.

Frequently Asked Questions about avoid-numeric-index

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why should I avoid numeric index signatures in TypeScript?

Avoid numeric index signatures because JavaScript runtime keys are always strings, making number indices a compile-time fiction that causes Object.keys behavior mismatches.

What should I use instead of a numeric index signature for collections in TypeScript?

Instead of a numeric index signature, use Array<T>, tuple, ArrayLike<T>, or Map<number, T> to model collections and ensure correct runtime semantics.

How do I replace { [n: number]: T } with correct TypeScript types?

Replace { [n: number]: T } by substituting Array<T>, tuple, ArrayLike<T>, or Map<number, T> depending on whether you model sequences, fixed-length data, or keyed collections.

Does Object.keys work differently on arrays versus numeric index signatures at runtime?

Object.keys returns string indices at runtime, but numeric index signatures imply number keys at compile time, creating a mismatch that Array<T> correctly models.

When do I need ArrayLike<T> instead of Map<number, T> in TypeScript?

Use ArrayLike<T> for array-like structures with length and indexed access, and Map<number, T> for keyed collections relying on numeric keys, ensuring correct runtime behavior.

What are the limitations of using numeric index signatures in TypeScript?

Numeric index signatures fail to align types with JavaScript runtime behavior, causing Object.keys to return strings instead of expected numbers, leading to logic errors.