{"record":{"id":"a3e3899961168432","repo":"FuelLabs/fuels-ts","slug":"encode-error","errorCode":"ENCODE_ERROR","errorMessage":"Expected array value.","messagePattern":"Expected array value\\.","errorType":"error_code","errorClass":"FuelError","httpStatus":null,"severity":"error","filePath":"packages/abi-coder/src/encoding/coders/ArrayCoder.ts","lineNumber":30,"sourceCode":"\nexport class ArrayCoder<TCoder extends Coder> extends Coder<\n  InputValueOf<TCoder>,\n  DecodedValueOf<TCoder>\n> {\n  coder: TCoder;\n  length: number;\n  #hasNestedOption: boolean;\n\n  constructor(coder: TCoder, length: number) {\n    super('array', `[${coder.type}; ${length}]`, length * coder.encodedLength);\n    this.coder = coder;\n    this.length = length;\n    this.#hasNestedOption = hasNestedOption([coder]);\n  }\n\n  encode(value: InputValueOf<TCoder>): Uint8Array {\n    if (!Array.isArray(value)) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Expected array value.`);\n    }\n\n    if (this.length !== value.length) {\n      throw new FuelError(ErrorCode.ENCODE_ERROR, `Types/values length mismatch.`);\n    }\n\n    return concat(Array.from(value).map((v) => this.coder.encode(v)));\n  }\n\n  decode(data: Uint8Array, offset: number): [DecodedValueOf<TCoder>, number] {\n    if ((!this.#hasNestedOption && data.length < this.encodedLength) || data.length > MAX_BYTES) {\n      throw new FuelError(ErrorCode.DECODE_ERROR, `Invalid array data size.`);\n    }\n\n    let newOffset = offset;\n    const decodedValue = Array(this.length)\n      .fill(0)\n      .map(() => {","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/FuelLabs/fuels-ts/blob/b3f37c91aca4aa9d5e4c0d3967f66237190826ea/packages/abi-coder/src/encoding/coders/ArrayCoder.ts#L12-L48","documentation":"Thrown by `ArrayCoder.encode` (code `ENCODE_ERROR`) when the value passed for a Sway fixed-size array type is not a JavaScript array. Sway arrays (`[T; N]`) must be encoded from an ordered JS array; anything else (object, string, number, BN) is rejected up front.","triggerScenarios":"Passing an object `{0: .., 1: ..}` or a `Uint8Array`-typed value where a plain array is expected; passing a single value where the ABI declares `[T; N]`; bypassing TS types with `as any`.","commonSituations":"Serializing from JSON that produced array-like objects instead of arrays; mixing up `Vec<T>` (which maps differently) and `[T; N]`; using a Set/Map where an array was expected.","solutions":["Pass a plain JS array: `[v1, v2, ...]`.","If you have an array-like object, convert with `Array.from(obj)` first.","Let generated TypeScript types guide construction instead of bypassing with `as any`."],"exampleFix":"// before — ABI: function foo([u64; 2])\nawait contract.functions.foo({ 0: 1, 1: 2 }).call();\n// after\nawait contract.functions.foo([1, 2]).call();","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(value)) {\n  throw new TypeError(`Expected an array for [T; N] input, got ${typeof value}`);\n}","typeGuard":"function isArrayValue(value) {\n  return Array.isArray(value);\n}","tryCatchPattern":"try {\n  arrayCoder.encode(value);\n} catch (e) {\n  if (e.code === 'ENCODE_ERROR' && /Expected array value/.test(e.message)) {\n    value = Array.from(value); // attempt normalize, then retry once\n  } else throw e;\n}","preventionTips":["Always construct Sway array inputs as JS arrays.","Let generated TS types (tuple/array) enforce the shape at compile time.","Normalize array-like objects with `Array.from` before encoding."],"tags":["abi-coder","encoding","array","typescript"],"backgroundTag":null,"analyzedSha":"b3f37c91aca4aa9d5e4c0d3967f66237190826ea","analyzedAt":"2026-08-12T20:30:56.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}