plotly/plotly.js · warning

bad argument to arrayDefaultKey: <name>

Error message

bad argument to arrayDefaultKey: <name>

What it means

arrayDefaultKey(name) derives a plural-to-singular defaults key (e.g. 'annotations' -> 'annotationdefaults') by chopping the last character. It warns when the given name does not end in 's', because the result would almost certainly not be the intended defaults attribute name. This is an internal template/defaults helper, so the warning usually points at schema/attribute wiring, not user layout code.

Source

Thrown at src/plot_api/plot_template.js:233

            }
        }
        return out;
    }

    return {
        newItem: newItem,
        defaultItems: defaultItems
    };
};

function validItemName(name) {
    return name && typeof name === 'string';
}

function arrayDefaultKey(name) {
    var lastChar = name.length - 1;
    if(name.charAt(lastChar) !== 's') {
        Lib.warn('bad argument to arrayDefaultKey: ' + name);
    }
    return name.slice(0, -1) + 'defaults';
}
exports.arrayDefaultKey = arrayDefaultKey;

/**
 * arrayEditor: helper for editing array items that may have come from
 *     template defaults (in which case they will not exist in the input yet)
 *
 * @param {object} parentIn: the input container (eg gd.layout)
 * @param {string} containerStr: the attribute string for the container inside
 *     `parentIn`.
 * @param {object} itemOut: the _full* item (eg gd._fullLayout.annotations[0])
 *     that we'll be editing. Assumed to have been created by `arrayTemplater`.
 *
 * @returns {object}: {modifyBase, modifyItem, getUpdateObj, applyUpdate}, all functions:
 *     modifyBase(attr, value): Add an update that's *not* related to the item.
 *         `attr` is the full attribute string.

View on GitHub (pinned to 1d090e0b5f)

Solutions

  1. Pass the plural attribute name ending in 's', e.g. arrayDefaultKey('annotations') -> 'annotationdefaults'.
  2. Fix the container name in your defaultsTemplate wiring so it is the standard plural form used in the plot schema.
  3. If the container genuinely has no plural form, do not use arrayDefaultKey; provide the defaults key explicitly.

Example fix

// before
arrayDefaultKey('annotation');
// after
arrayDefaultKey('annotations'); // -> 'annotationdefaults'
Defensive patterns

Strategy: validation

Validate before calling

function checkPluralName(name) {
  if (typeof name !== 'string' || !name.endsWith('s')) throw new Error(`arrayDefaultKey expects a plural name ending in 's', got: ${name}`);
  return name;
}
arrayDefaultKey(checkPluralName('annotations'));

Type guard

const isPluralString = (n) => typeof n === 'string' && n.length > 0 && n.endsWith('s');

Prevention

When it happens

Trigger: Calling arrayDefaultKey with a name that does not end in 's', e.g. arrayDefaultKey('shape') or arrayDefaultKey('layout'), typically from a custom template or defaults code (defaultsTemplate) registering an array container whose plural name is misformed.

Common situations: Authors of custom trace/layout templates or plugins wiring new array containers; typos in container names inside defaults template code.


AI-assisted analysis of plotly/plotly.js@1d090e0b5f (2026-09-02). Data as JSON: /api/errors/4159c08f76ab64c6. Report an issue: GitHub.