evanw/esbuild · error · Error

Should be array of strings

Error message

Should be array of strings

What it means

Thrown by read_string() in scripts/terser-tests.js:280. When a test field value is an array literal, every element must be an AST_String. If any element is a number, identifier, template literal, or other expression, the `.map` callback throws this error.

Source

Thrown at scripts/terser-tests.js:280

    if (stat.TYPE == "SimpleStatement") {
      var body = stat.body;
      if (body instanceof U.AST_Boolean) {
        return body.value;
      }
    }
    throw new Error("Should be boolean");
  }

  function read_string(stat) {
    if (stat.TYPE == "SimpleStatement") {
      var body = stat.body;
      switch (body.TYPE) {
        case "String":
          return body.value;
        case "Array":
          return body.elements.map(function (element) {
            if (element.TYPE !== "String")
              throw new Error("Should be array of strings");
            return element.value;
          }).join("\n");
      }
    }
    throw new Error("Should be string or array of strings");
  }

  function get_one_test(name, block) {
    var test = {
      name: name,
      options: {},
      reminify: true,
    };
    var tw = new U.TreeWalker(function (node, descend) {
      if (node instanceof U.AST_Assign) {
        if (!(node.left instanceof U.AST_SymbolRef)) {
          croak(node);
        }

View on GitHub (pinned to f6058f8364)

Solutions

  1. Make every element of the array a double- or single-quoted string literal.
  2. If you need a non-string value, move it to the correct field type (e.g. a boolean field) instead of the string array.

Example fix

// before
expect: ["var a = 1;", 2, "console.log(a);"];

// after
expect: ["var a = 1;", "console.log(a);"];
Defensive patterns

Strategy: type-guard

Validate before calling

function allElementsAreStrings(arrNode) {
  return arrNode instanceof U.AST_Array &&
    arrNode.elements.every(el => el instanceof U.AST_String);
}

Type guard

const isStringArray = (node) =>
  node instanceof U.AST_Array && node.elements.every(el => el.TYPE === 'String');

Try / catch

try { read_string(stat); }
catch (e) {
  if (/Should be array of strings/.test(e.message)) console.error('Every array element must be a string literal');
  throw e;
}

Prevention

When it happens

Trigger: Writing a multi-line string field as an array that contains a non-string element, e.g. `expect: ["a", 42, "b"];` or `expect: ["a", foo];`. The array form is only valid when all elements are plain string literals (they get joined with newlines).

Common situations: A contributor mixes a variable reference or numeric value into what should be an array of string snippets, often when splitting a long expected output across lines.

Related errors


AI-assisted analysis of evanw/esbuild@f6058f8364 (2026-08-09). Data as JSON: /api/errors/f50d36d6ed5c7648. Report an issue: GitHub.