{"record":{"id":"149bcea66712d155","repo":"codex-team/editor.js","slug":"index-should-be-a-number","errorCode":null,"errorMessage":"Index should be a number","messagePattern":"Index should be a number","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/components/modules/api/blocks.ts","lineNumber":417,"sourceCode":"        data,\n      });\n    });\n\n    this.Editor.BlockManager.insertMany(blocksToInsert, index);\n\n    // we cast to any because our BlockAPI has no \"new\" signature\n    // eslint-disable-next-line @typescript-eslint/no-explicit-any\n    return blocksToInsert.map((block) => new (BlockAPI as any)(block));\n  };\n\n  /**\n   * Validated block index and throws an error if it's invalid\n   *\n   * @param index - index to validate\n   */\n  private validateIndex(index: unknown): void {\n    if (typeof index !== 'number') {\n      throw new Error('Index should be a number');\n    }\n\n    if (index < 0) {\n      throw new Error(`Index should be greater than or equal to 0`);\n    }\n\n    if (index === null) {\n      throw new Error(`Index should be greater than or equal to 0`);\n    }\n  }\n}\n","sourceCodeStart":399,"sourceCodeEnd":429,"githubUrl":"https://github.com/codex-team/editor.js/blob/5f45dabbe5690b7033b2f9047d3985add5045fdd/src/components/modules/api/blocks.ts#L399-L429","documentation":"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.","triggerScenarios":"Calling editor.blocks.insertMany(blocks, '2'), passing undefined for the index argument, or sourcing the index from DOM dataset/URL params as a string.","commonSituations":"Index read from data attributes or query strings (always strings); optional-index code paths that forward undefined; refactors that changed the parameter type.","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"],"exampleFix":"// before\neditor.blocks.insertMany(blocks, blockEl.dataset.index);\n// after\neditor.blocks.insertMany(blocks, Number(blockEl.dataset.index));","handlingStrategy":"type-guard","validationCode":"if (typeof index !== 'number' || Number.isNaN(index)) index = editor.blocks.getBlocksAmount(); editor.blocks.insertMany(blocks, index);","typeGuard":"const isNumberIndex = (i: unknown): i is number => typeof i === 'number' && !Number.isNaN(i);","tryCatchPattern":"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; }","preventionTips":["Coerce indices from DOM/URL sources with Number()","Type the parameter as number in your wrapper"],"tags":["insert-many","index","type-validation","editorjs"],"backgroundTag":"invalid-argument-type","analyzedSha":"5f45dabbe5690b7033b2f9047d3985add5045fdd","analyzedAt":"2026-08-27T22:50:23.124Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}