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
- Always pass an index: store.key(0), store.key(1), ... and stop when the result is null.
- Guard before calling: if (i !== undefined) store.key(i).
- 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
- Always call key() with an explicit integer index.
- Iterate keys with a while (store.key(i) !== null) loop from i=0.
- Mirror Web Storage API signatures when wrapping the store.
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
- Cannot create a link without a resource. You must provide th
- You must provide a key to remove an item from the store
AI-assisted analysis of marmelab/react-admin@051f511bb0 (2026-08-30).
Data as JSON: /api/errors/821245f572698747.
Report an issue: GitHub.