marmelab/react-admin · error · TypeError

Failed to execute 'key' on 'Storage': 1 argument required, b

Error message

Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present.

What it means

react-admin's localStorageStore includes a localStorage-compatible InMemoryStorage shim. Its key(i) method mimics the Web Storage API, which requires exactly one argument, so it throws a TypeError when called with zero arguments — matching Chrome's native behavior so code that expects the platform error behaves the same in memory-only environments (SSR, tests, private-mode fallbacks).

Source

Thrown at packages/ra-core/src/store/localStorageStore.ts:211

    removeItem(key: string) {
        this.valuesMap.delete(key);
    }

    removeItems(keyPrefix: string) {
        this.valuesMap.forEach((value, key) => {
            if (key.startsWith(keyPrefix)) {
                this.valuesMap.delete(key);
            }
        });
    }

    clear() {
        this.valuesMap.clear();
    }

    key(i): string {
        if (arguments.length === 0) {
            throw new TypeError(
                "Failed to execute 'key' on 'Storage': 1 argument required, but only 0 present."
            ); // this is a TypeError implemented on Chrome, Firefox throws Not enough arguments to Storage.key.
        }
        const arr = Array.from(this.valuesMap.keys()) as string[];
        return arr[i];
    }

    get length() {
        return this.valuesMap.size;
    }
}
const memoryStorage = new LocalStorageShim();

export const getStorage = () => {
    return localStorageAvailable ? window.localStorage : memoryStorage;
};

View on GitHub (pinned to 051f511bb0)

Solutions

  1. Always pass an index: store.key(0), store.key(1), ... and stop when the result is null.
  2. Guard before calling: if (i !== undefined) store.key(i).
  3. If you need all keys, iterate with key(i) from i=0 while non-null instead of calling key() without arguments.

Example fix

// before
const firstKey = localStorageStore.key(); // TypeError

// after
const firstKey = localStorageStore.key(0); // may be null if empty
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof index === 'undefined') {
    throw new Error('Storage.key requires an index');
}
const key = store.key(index);

Type guard

const hasIndex = (i: number | undefined): i is number => typeof i === 'number' && i >= 0;

Try / catch

try {
    key = store.key(i);
} catch (e) {
    if (e instanceof TypeError) console.error('key() requires an index');
    key = null;
}

Prevention

When it happens

Trigger: Calling localStorageStore.key() with no argument (or spreading an empty argument list) into the store object returned when window.localStorage is unavailable; generic code iterating Storage keys that omits the index on first iteration.

Common situations: SSR/test environments where window.localStorage is undefined so the in-memory shim is used; browser privacy mode fallbacks; porting code that worked with native Storage error messages and relying on the Chrome-style TypeError text.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30). Data as JSON: /api/errors/821245f572698747. Report an issue: GitHub.