Skip to content

Commit 549bad4

Browse files
committed
Improved typing for deriveStateFromMetadata
1 parent 0339b01 commit 549bad4

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

packages/base-controller/src/BaseControllerV2.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -247,29 +247,30 @@ function deriveStateFromMetadata<S extends Record<string, Json>>(
247247
metadata: StateMetadata<S>,
248248
metadataProperty: 'anonymous' | 'persist',
249249
): Record<string, Json> {
250-
return Object.keys(state).reduce((persistedState, key) => {
251-
try {
252-
const stateMetadata = metadata[key as keyof S];
253-
if (!stateMetadata) {
254-
throw new Error(`No metadata found for '${key}'`);
250+
return Object.keys(state).reduce(
251+
(persistedState: Record<string, Json>, key: keyof S) => {
252+
try {
253+
const stateMetadata = metadata[key];
254+
if (!stateMetadata) {
255+
throw new Error(`No metadata found for '${String(key)}'`);
256+
}
257+
const propertyMetadata = stateMetadata[metadataProperty];
258+
const stateProperty = state[key];
259+
if (typeof propertyMetadata === 'function') {
260+
persistedState[String(key)] = propertyMetadata(stateProperty);
261+
} else if (propertyMetadata) {
262+
persistedState[String(key)] = stateProperty;
263+
}
264+
return persistedState;
265+
} catch (error) {
266+
// Throw error after timeout so that it is captured as a console error
267+
// (and by Sentry) without interrupting state-related operations
268+
setTimeout(() => {
269+
throw error;
270+
});
271+
return persistedState;
255272
}
256-
const propertyMetadata = stateMetadata[metadataProperty];
257-
const stateProperty = state[key];
258-
if (typeof propertyMetadata === 'function') {
259-
persistedState[key as string] = propertyMetadata(
260-
stateProperty as S[keyof S],
261-
);
262-
} else if (propertyMetadata) {
263-
persistedState[key as string] = stateProperty;
264-
}
265-
return persistedState;
266-
} catch (error) {
267-
// Throw error after timeout so that it is captured as a console error
268-
// (and by Sentry) without interrupting state-related operations
269-
setTimeout(() => {
270-
throw error;
271-
});
272-
return persistedState;
273-
}
274-
}, {} as Record<string, Json>);
273+
},
274+
{},
275+
);
275276
}

0 commit comments

Comments
 (0)