Numeric Index Signatures For Slots
Numeric index signatures type array-like records by position, while runtime keys stay strings — Object.keys returns string keys even for ids.
Code
// Numeric index signatures type access by number position
type Slots = { [index: number]: string | undefined };
const slots: Slots = [];
slots[2] = 'charlie';
console.log('slot 2:', slots[2]);
console.log('slot 0:', slots[0] ?? 'empty');
// Numeric keys stringify at runtime — Object.keys always returns strings
const names: Record<number, string> = { 7: 'delta' };
console.log('id 7:', names[7]);
const keyList = Object.keys(names);
console.log('runtime keys:', keyList.join(','), '| first key type:', typeof keyList[0]);Output
Captured from a sandboxed run of this snippet in the build pipeline.
slot 2: charlie
slot 0: empty
id 7: delta
runtime keys: 7 | first key type: stringWalkthrough
Top-level bindings keyList hold the demo state — nothing sits inside a function scope.
What to watch for
- Each symbol it names is a language built-in — nothing to download.
Putting it to use
The run emits 4 labeled sections — slot 2:, slot 0:, id 7:, runtime keys: — one per step.
Core lines
const names: Record<number, string> = { 7: 'delta' };
const keyList = Object.keys(names);
const slots: Slots = [];
Interactive Tools for This Topic
FAQ
- Does the code pull in any external packages?
- No external packages are involved; every call resolves to a built-in API. Its state lives in module-level bindings: `keyList`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.