codex-team/editor.js · error · Error
Index should be a number
Error message
Index should be a number
What it means
blocks.insertMany(blocks, index) validates the insertion index via validateIndex, which requires it to be a number. Passing a string, undefined, null, or NaN triggers this error before any insertion happens.
Source
Thrown at src/components/modules/api/blocks.ts:417
data,
});
});
this.Editor.BlockManager.insertMany(blocksToInsert, index);
// we cast to any because our BlockAPI has no "new" signature
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return blocksToInsert.map((block) => new (BlockAPI as any)(block));
};
/**
* Validated block index and throws an error if it's invalid
*
* @param index - index to validate
*/
private validateIndex(index: unknown): void {
if (typeof index !== 'number') {
throw new Error('Index should be a number');
}
if (index < 0) {
throw new Error(`Index should be greater than or equal to 0`);
}
if (index === null) {
throw new Error(`Index should be greater than or equal to 0`);
}
}
}
View on GitHub (pinned to 5f45dabbe5)
Solutions
- Coerce/Number()-validate the index before calling insertMany
- Pass a literal number or omit-dependent default computed as number
- Add a type guard isNumber(index) in the calling code
Example fix
// before editor.blocks.insertMany(blocks, blockEl.dataset.index); // after editor.blocks.insertMany(blocks, Number(blockEl.dataset.index));
Defensive patterns
Strategy: type-guard
Validate before calling
if (typeof index !== 'number' || Number.isNaN(index)) index = editor.blocks.getBlocksAmount(); editor.blocks.insertMany(blocks, index);
Type guard
const isNumberIndex = (i: unknown): i is number => typeof i === 'number' && !Number.isNaN(i);
Try / catch
try { editor.blocks.insertMany(blocks, index); } catch (e) { if (e instanceof Error && e.message === 'Index should be a number') { editor.blocks.insertMany(blocks, Number(index)); return; } throw e; } Prevention
- Coerce indices from DOM/URL sources with Number()
- Type the parameter as number in your wrapper
When it happens
Trigger: Calling editor.blocks.insertMany(blocks, '2'), passing undefined for the index argument, or sourcing the index from DOM dataset/URL params as a string.
Common situations: Index read from data attributes or query strings (always strings); optional-index code paths that forward undefined; refactors that changed the parameter type.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Index should be greater than or equal to 0
- Unable to move Block down since it is already the last
- Unable to move Block up since it is already the first
- Incorrect data passed to the render() method
- Block with id "${id}" not found
AI-assisted analysis of codex-team/editor.js@5f45dabbe5 (2026-08-27).
Data as JSON: /api/errors/149bcea66712d155.
Report an issue: GitHub.