From ace9abf8dd88d4ad6b166cd648243deaeb07f9ab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Mon, 19 Jan 2026 14:05:32 +0000
Subject: [PATCH 1/8] Add unit test to cover bug with collection member key and
 Onyx.clear()

---
 tests/unit/useOnyxTest.ts | 39 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/tests/unit/useOnyxTest.ts b/tests/unit/useOnyxTest.ts
index 1438f5b08..307f11221 100644
--- a/tests/unit/useOnyxTest.ts
+++ b/tests/unit/useOnyxTest.ts
@@ -236,7 +236,7 @@ describe('useOnyx', () => {
             expect(result2.current[1].status).toEqual('loaded');
         });
 
-        it('should return updated state when connecting to the same key after an Onyx.clear() call', async () => {
+        it('should return updated state when connecting to the same regular key after an Onyx.clear() call', async () => {
             await StorageMock.setItem(ONYXKEYS.TEST_KEY, 'test');
 
             const {result: result1} = renderHook(() => useOnyx(ONYXKEYS.TEST_KEY));
@@ -270,6 +270,43 @@ describe('useOnyx', () => {
             expect(result3.current[0]).toEqual('test2');
             expect(result3.current[1].status).toEqual('loaded');
         });
+
+        it('should return updated state when connecting to the same colection member key after an Onyx.clear() call', async () => {
+            // @ts-expect-error bypass
+            await StorageMock.setItem(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, 'test');
+
+            const {result: result1} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
+
+            await act(async () => waitForPromisesToResolve());
+
+            expect(result1.current[0]).toEqual('test');
+            expect(result1.current[1].status).toEqual('loaded');
+
+            await act(async () => Onyx.clear());
+
+            const {result: result2} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
+            const {result: result3} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
+
+            await act(async () => waitForPromisesToResolve());
+            await act(async () => waitForPromisesToResolve());
+
+            expect(result1.current[0]).toBeUndefined();
+            expect(result1.current[1].status).toEqual('loaded');
+            expect(result2.current[0]).toBeUndefined();
+            expect(result2.current[1].status).toEqual('loaded');
+            expect(result3.current[0]).toBeUndefined();
+            expect(result3.current[1].status).toEqual('loaded');
+
+            Onyx.merge(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, 'test2');
+            await act(async () => waitForPromisesToResolve());
+
+            expect(result1.current[0]).toEqual('test2');
+            expect(result1.current[1].status).toEqual('loaded');
+            expect(result2.current[0]).toEqual('test2');
+            expect(result2.current[1].status).toEqual('loaded');
+            expect(result3.current[0]).toEqual('test2');
+            expect(result3.current[1].status).toEqual('loaded');
+        });
     });
 
     describe('selector', () => {

From c9555fc0e7e89767d90ba2cdd5ef38c3c29e4645 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Wed, 21 Jan 2026 16:45:09 +0000
Subject: [PATCH 2/8] Fix stale onyx collection member key data when clearing

---
 lib/Onyx.ts | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/lib/Onyx.ts b/lib/Onyx.ts
index bfae1c739..c61978de3 100644
--- a/lib/Onyx.ts
+++ b/lib/Onyx.ts
@@ -9,7 +9,6 @@ import type {
     InitOptions,
     KeyValueMapping,
     OnyxInputKeyValueMapping,
-    OnyxCollection,
     MixedOperationsQueue,
     OnyxKey,
     OnyxMergeCollectionInput,
@@ -309,9 +308,11 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
             cache.clearNullishStorageKeys();
 
             const keysToBeClearedFromStorage: OnyxKey[] = [];
-            const keyValuesToResetAsCollection: Record<OnyxKey, OnyxCollection<KeyValueMapping[OnyxKey]>> = {};
+            const keyValuesToResetAsCollection: Record<
+                OnyxKey,
+                {oldValues: Record<string, KeyValueMapping[OnyxKey] | undefined>; newValues: Record<string, KeyValueMapping[OnyxKey] | undefined>}
+            > = {};
             const keyValuesToResetIndividually: KeyValueMapping = {};
-
             const allKeys = new Set([...cachedKeys, ...initialKeys]);
 
             // The only keys that should not be cleared are:
@@ -344,9 +345,10 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
 
                         if (collectionKey) {
                             if (!keyValuesToResetAsCollection[collectionKey]) {
-                                keyValuesToResetAsCollection[collectionKey] = {};
+                                keyValuesToResetAsCollection[collectionKey] = {oldValues: {}, newValues: {}};
                             }
-                            keyValuesToResetAsCollection[collectionKey]![key] = newValue ?? undefined;
+                            keyValuesToResetAsCollection[collectionKey].oldValues[key] = oldValue;
+                            keyValuesToResetAsCollection[collectionKey].newValues[key] = newValue ?? undefined;
                         } else {
                             keyValuesToResetIndividually[key] = newValue ?? undefined;
                         }
@@ -368,7 +370,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
                 updatePromises.push(OnyxUtils.scheduleSubscriberUpdate(key, value));
             }
             for (const [key, value] of Object.entries(keyValuesToResetAsCollection)) {
-                updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value));
+                updatePromises.push(OnyxUtils.scheduleNotifyCollectionSubscribers(key, value.newValues, value.oldValues));
             }
 
             const defaultKeyValuePairs = Object.entries(

From 905afd013bd0784310222e6a52db846d551cd514 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Wed, 21 Jan 2026 16:45:32 +0000
Subject: [PATCH 3/8] Remove extra act

---
 tests/unit/useOnyxTest.ts | 1 -
 1 file changed, 1 deletion(-)

diff --git a/tests/unit/useOnyxTest.ts b/tests/unit/useOnyxTest.ts
index 307f11221..d6353ed70 100644
--- a/tests/unit/useOnyxTest.ts
+++ b/tests/unit/useOnyxTest.ts
@@ -287,7 +287,6 @@ describe('useOnyx', () => {
             const {result: result2} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
             const {result: result3} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
 
-            await act(async () => waitForPromisesToResolve());
             await act(async () => waitForPromisesToResolve());
 
             expect(result1.current[0]).toBeUndefined();

From dfbe7d88cb78801b1f0dfcb94f784275eaab1252 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Wed, 21 Jan 2026 16:45:48 +0000
Subject: [PATCH 4/8] Remove unnecessary test skip in IDB

---
 tests/unit/storage/providers/IDBKeyvalProviderTest.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/unit/storage/providers/IDBKeyvalProviderTest.ts b/tests/unit/storage/providers/IDBKeyvalProviderTest.ts
index bb29ec9a4..57470adc5 100644
--- a/tests/unit/storage/providers/IDBKeyvalProviderTest.ts
+++ b/tests/unit/storage/providers/IDBKeyvalProviderTest.ts
@@ -67,7 +67,7 @@ describe('IDBKeyValProvider', () => {
             expect(await IDB.get(ONYXKEYS.TEST_KEY, IDBKeyValProvider.store)).toEqual('value');
         });
 
-        it.skip('should remove the key when passing null', async () => {
+        it('should remove the key when passing null', async () => {
             await IDBKeyValProvider.setItem(ONYXKEYS.TEST_KEY, 'value');
             expect(await IDB.get(ONYXKEYS.TEST_KEY, IDBKeyValProvider.store)).toEqual('value');
 

From 927f60fac36de145f6efd95a928b72b5458fd419 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Thu, 22 Jan 2026 13:44:59 +0000
Subject: [PATCH 5/8] Remove TS bypasses

---
 tests/perf-test/OnyxUtils.perf-test.ts | 12 ++---
 tests/perf-test/useOnyx.perf-test.tsx  |  7 ++-
 tests/unit/useOnyxTest.ts              | 69 ++++++++++----------------
 3 files changed, 35 insertions(+), 53 deletions(-)

diff --git a/tests/perf-test/OnyxUtils.perf-test.ts b/tests/perf-test/OnyxUtils.perf-test.ts
index 2dd9b7f4e..9005287ce 100644
--- a/tests/perf-test/OnyxUtils.perf-test.ts
+++ b/tests/perf-test/OnyxUtils.perf-test.ts
@@ -9,7 +9,7 @@ import OnyxUtils, {clearOnyxUtilsInternals} from '../../lib/OnyxUtils';
 import type GenericCollection from '../utils/GenericCollection';
 import type {OnyxUpdate} from '../../lib/Onyx';
 import createDeferredTask from '../../lib/createDeferredTask';
-import type {OnyxInputKeyValueMapping, OnyxKey, RetriableOnyxOperation} from '../../lib/types';
+import type {OnyxEntry, OnyxInputKeyValueMapping, OnyxKey, RetriableOnyxOperation} from '../../lib/types';
 
 const ONYXKEYS = {
     TEST_KEY: 'test',
@@ -31,11 +31,11 @@ const evictableKeys = [ONYXKEYS.COLLECTION.EVICTABLE_TEST_KEY];
 
 const initialKeyStates = {};
 
-// @ts-expect-error bypass
-const generateTestSelector = (): Selector<string, unknown, unknown> => (value: Record<string, unknown>) => ({
-    reportActionID: value.reportActionID,
-    originalMessage: value.originalMessage,
-});
+const generateTestSelector = () =>
+    ((value: OnyxEntry<Record<string, unknown>>) => ({
+        reportActionID: value?.reportActionID,
+        originalMessage: value?.originalMessage,
+    })) as Selector<OnyxKey, {reportActionID?: string; originalMessage?: string}>;
 
 const collectionKey = ONYXKEYS.COLLECTION.TEST_KEY;
 const mockedReportActionsMap = getRandomReportActions(collectionKey);
diff --git a/tests/perf-test/useOnyx.perf-test.tsx b/tests/perf-test/useOnyx.perf-test.tsx
index d59defa9a..78672203b 100644
--- a/tests/perf-test/useOnyx.perf-test.tsx
+++ b/tests/perf-test/useOnyx.perf-test.tsx
@@ -5,6 +5,7 @@ import {measureRenders} from 'reassure';
 import type {FetchStatus, OnyxEntry, OnyxKey, OnyxValue, ResultMetadata, UseOnyxOptions} from '../../lib';
 import Onyx, {useOnyx} from '../../lib';
 import StorageMock from '../../lib/storage';
+import type {UseOnyxSelector} from '../../lib/useOnyx';
 
 const ONYXKEYS = {
     TEST_KEY: 'test',
@@ -142,8 +143,7 @@ describe('useOnyx', () => {
                 <UseOnyxWrapper
                     onyxKey={key}
                     onyxOptions={{
-                        // @ts-expect-error bypass
-                        selector: (entry: OnyxEntry<{id: string; name: string}>) => `${entry?.name}_changed`,
+                        selector: ((entry: OnyxEntry<{id: string; name: string}>) => `${entry?.name}_changed`) as UseOnyxSelector<OnyxKey, string>,
                     }}
                 />,
                 {
@@ -173,8 +173,7 @@ describe('useOnyx', () => {
                 <UseOnyxWrapper
                     onyxKey={key}
                     onyxOptions={{
-                        // @ts-expect-error bypass
-                        selector: (entry: OnyxEntry<{id: string; name: string}>) => `${entry?.name}_changed`,
+                        selector: ((entry: OnyxEntry<{id: string; name: string}>) => `${entry?.name}_changed`) as UseOnyxSelector<OnyxKey, string>,
                     }}
                 />,
                 {
diff --git a/tests/unit/useOnyxTest.ts b/tests/unit/useOnyxTest.ts
index d6353ed70..19fe2cd09 100644
--- a/tests/unit/useOnyxTest.ts
+++ b/tests/unit/useOnyxTest.ts
@@ -1,5 +1,5 @@
 import {act, renderHook} from '@testing-library/react-native';
-import type {OnyxCollection, OnyxEntry} from '../../lib';
+import type {OnyxCollection, OnyxEntry, OnyxKey} from '../../lib';
 import Onyx, {useOnyx} from '../../lib';
 import OnyxCache from '../../lib/OnyxCache';
 import StorageMock from '../../lib/storage';
@@ -7,6 +7,7 @@ import type GenericCollection from '../utils/GenericCollection';
 import waitForPromisesToResolve from '../utils/waitForPromisesToResolve';
 import * as Logger from '../../lib/Logger';
 import onyxSnapshotCache from '../../lib/OnyxSnapshotCache';
+import type {UseOnyxSelector} from '../../lib/useOnyx';
 
 const ONYXKEYS = {
     TEST_KEY: 'test',
@@ -272,8 +273,7 @@ describe('useOnyx', () => {
         });
 
         it('should return updated state when connecting to the same colection member key after an Onyx.clear() call', async () => {
-            // @ts-expect-error bypass
-            await StorageMock.setItem(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, 'test');
+            await StorageMock.setItem<string>(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`, 'test');
 
             const {result: result1} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}entry1`));
 
@@ -314,8 +314,7 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`,
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`) as UseOnyxSelector<OnyxKey, string>,
                 }),
             );
 
@@ -337,12 +336,11 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.COLLECTION.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entries: OnyxCollection<{id: string; name: string}>) =>
+                    selector: ((entries: OnyxCollection<{id: string; name: string}>) =>
                         Object.entries(entries ?? {}).reduce<NonNullable<OnyxCollection<string>>>((acc, [key, value]) => {
                             acc[key] = value?.id;
                             return acc;
-                        }, {}),
+                        }, {})) as UseOnyxSelector<OnyxKey, NonNullable<OnyxCollection<string>>>,
                 }),
             );
 
@@ -370,24 +368,21 @@ describe('useOnyx', () => {
             // primitive
             const {result: primitiveResult} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => entry?.id,
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => entry?.id) as UseOnyxSelector<OnyxKey, string>,
                 }),
             );
 
             // object
             const {result: objectResult} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => ({id: entry?.id}),
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => ({id: entry?.id})) as UseOnyxSelector<OnyxKey, {id?: string}>,
                 }),
             );
 
             // array
             const {result: arrayResult} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => [{id: entry?.id}],
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => [{id: entry?.id}]) as UseOnyxSelector<OnyxKey, Array<{id?: string}>>,
                 }),
             );
 
@@ -414,8 +409,7 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.COLLECTION.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => ({id: entry?.id}),
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => ({id: entry?.id})) as UseOnyxSelector<OnyxKey, {id?: string}>,
                 }),
             );
 
@@ -432,11 +426,10 @@ describe('useOnyx', () => {
         it('should always use the current selector reference to return new data', async () => {
             Onyx.set(ONYXKEYS.TEST_KEY, {id: 'test_id', name: 'test_name'});
 
-            let selector = (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`;
+            let selector = ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name}`) as UseOnyxSelector<OnyxKey, string>;
 
             const {result, rerender} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
                     selector,
                 }),
             );
@@ -444,7 +437,7 @@ describe('useOnyx', () => {
             expect(result.current[0]).toEqual('id - test_id, name - test_name');
             expect(result.current[1].status).toEqual('loaded');
 
-            selector = (entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed`;
+            selector = ((entry: OnyxEntry<{id: string; name: string}>) => `id - ${entry?.id}, name - ${entry?.name} - selector changed`) as UseOnyxSelector<OnyxKey, string>;
             rerender(undefined);
 
             expect(result.current[0]).toEqual('id - test_id, name - test_name - selector changed');
@@ -456,11 +449,10 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string; count: number}>) => ({
+                    selector: ((entry: OnyxEntry<{id: string; name: string; count: number}>) => ({
                         id: entry?.id,
                         name: entry?.name,
-                    }),
+                    })) as UseOnyxSelector<OnyxKey, {id?: string; name?: string}>,
                 }),
             );
 
@@ -480,11 +472,10 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => ({
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => ({
                         id: entry?.id,
                         name: entry?.name,
-                    }),
+                    })) as UseOnyxSelector<OnyxKey, {id?: string; name?: string}>,
                 }),
             );
 
@@ -507,11 +498,10 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{id: string; name: string}>) => {
+                    selector: ((entry: OnyxEntry<{id: string; name: string}>) => {
                         selectorCallCount++;
                         return {id: entry?.id, name: entry?.name};
-                    },
+                    }) as UseOnyxSelector<OnyxKey, {id?: string; name?: string}>,
                 }),
             );
 
@@ -534,8 +524,7 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<{count: number; name: string}>) => entry?.count || 0,
+                    selector: ((entry: OnyxEntry<{count: number; name: string}>) => entry?.count || 0) as UseOnyxSelector<OnyxKey, number>,
                 }),
             );
 
@@ -757,8 +746,7 @@ describe('useOnyx', () => {
 
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<string>) => `${entry}_changed`,
+                    selector: ((entry: OnyxEntry<string>) => `${entry}_changed`) as UseOnyxSelector<OnyxKey, string>,
                 }),
             );
 
@@ -813,8 +801,7 @@ describe('useOnyx', () => {
             const {result} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
                     initWithStoredValues: false,
-                    // @ts-expect-error bypass
-                    selector: (value: OnyxEntry<string>) => `${value}_selected`,
+                    selector: ((value: OnyxEntry<string>) => `${value}_selected`) as UseOnyxSelector<OnyxKey, string>,
                 }),
             );
 
@@ -948,12 +935,11 @@ describe('useOnyx', () => {
                 useOnyx(
                     ONYXKEYS.COLLECTION.TEST_KEY,
                     {
-                        // @ts-expect-error bypass
-                        selector: (entries: OnyxCollection<{id: string; name: string}>) =>
+                        selector: ((entries: OnyxCollection<{id: string; name: string}>) =>
                             Object.entries(entries ?? {}).reduce<NonNullable<OnyxCollection<string>>>((acc, [key, value]) => {
                                 acc[key] = `${value?.id}_${externalValue}`;
                                 return acc;
-                            }, {}),
+                            }, {})) as UseOnyxSelector<OnyxKey, NonNullable<OnyxCollection<string>>>,
                     },
                     [externalValue],
                 ),
@@ -1017,8 +1003,7 @@ describe('useOnyx', () => {
         });
 
         it('should always return undefined when subscribing to a skippable collection member id', async () => {
-            // @ts-expect-error bypass
-            await StorageMock.setItem(`${ONYXKEYS.COLLECTION.TEST_KEY}skippable-id`, 'skippable-id_value');
+            await StorageMock.setItem<string>(`${ONYXKEYS.COLLECTION.TEST_KEY}skippable-id`, 'skippable-id_value');
 
             const {result} = renderHook(() => useOnyx(`${ONYXKEYS.COLLECTION.TEST_KEY}skippable-id`));
 
@@ -1106,8 +1091,7 @@ describe('useOnyx', () => {
         it('should log an alert if Onyx doesn\'t return selected data in loaded state and "canBeMissing" property is false', async () => {
             const {result: result1} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
-                    selector: (entry: OnyxEntry<string>) => (entry ? `${entry}_changed` : undefined),
+                    selector: ((entry: OnyxEntry<string>) => (entry ? `${entry}_changed` : undefined)) as UseOnyxSelector<OnyxKey, string | undefined>,
                     canBeMissing: false,
                 }),
             );
@@ -1137,9 +1121,8 @@ describe('useOnyx', () => {
         it('should log an alert if Onyx doesn\'t return data but there is a selector that always return something and "canBeMissing" property is false', async () => {
             const {result: result1} = renderHook(() =>
                 useOnyx(ONYXKEYS.TEST_KEY, {
-                    // @ts-expect-error bypass
                     // This selector will always return a value, even if the Onyx data is missing.
-                    selector: (entry: OnyxEntry<string>) => `${entry}_changed`,
+                    selector: ((entry: OnyxEntry<string>) => `${entry}_changed`) as UseOnyxSelector<OnyxKey, string>,
                     canBeMissing: false,
                 }),
             );

From faa42ae620c678a065560101e65c44b282a92129 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Thu, 22 Jan 2026 13:49:10 +0000
Subject: [PATCH 6/8] Add explanation for change

---
 lib/Onyx.ts | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lib/Onyx.ts b/lib/Onyx.ts
index c61978de3..98a3f3168 100644
--- a/lib/Onyx.ts
+++ b/lib/Onyx.ts
@@ -308,11 +308,14 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
             cache.clearNullishStorageKeys();
 
             const keysToBeClearedFromStorage: OnyxKey[] = [];
+            const keyValuesToResetIndividually: KeyValueMapping = {};
+            // We need to store old and new values for collection keys to properly notify subscribers when clearing Onyx
+            // because the notification process needs the old values in cache but at that point they will be already removed from it.
             const keyValuesToResetAsCollection: Record<
                 OnyxKey,
                 {oldValues: Record<string, KeyValueMapping[OnyxKey] | undefined>; newValues: Record<string, KeyValueMapping[OnyxKey] | undefined>}
             > = {};
-            const keyValuesToResetIndividually: KeyValueMapping = {};
+            
             const allKeys = new Set([...cachedKeys, ...initialKeys]);
 
             // The only keys that should not be cleared are:

From 72dc89ff0857f53774a072ec087217e572b42819 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Thu, 22 Jan 2026 13:52:22 +0000
Subject: [PATCH 7/8] Fix lint

---
 lib/useOnyx.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/useOnyx.ts b/lib/useOnyx.ts
index b60214d6a..b1670e3ce 100644
--- a/lib/useOnyx.ts
+++ b/lib/useOnyx.ts
@@ -330,7 +330,7 @@ function useOnyx<TKey extends OnyxKey, TReturnValue = OnyxValue<TKey>>(
         }
 
         return resultRef.current;
-    }, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector, cacheKey]);
+    }, [options?.initWithStoredValues, options?.allowStaleData, options?.canBeMissing, key, memoizedSelector, cacheKey, previousKey]);
 
     const subscribe = useCallback(
         (onStoreChange: () => void) => {

From 6b56e381b74ac7e406917f7d834d573b0b10645e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?F=C3=A1bio=20Henriques?= <contact@fabiohenriques.dev>
Date: Thu, 22 Jan 2026 13:54:19 +0000
Subject: [PATCH 8/8] Fix prettier

---
 lib/Onyx.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/Onyx.ts b/lib/Onyx.ts
index 98a3f3168..f9a4a8bdb 100644
--- a/lib/Onyx.ts
+++ b/lib/Onyx.ts
@@ -315,7 +315,7 @@ function clear(keysToPreserve: OnyxKey[] = []): Promise<void> {
                 OnyxKey,
                 {oldValues: Record<string, KeyValueMapping[OnyxKey] | undefined>; newValues: Record<string, KeyValueMapping[OnyxKey] | undefined>}
             > = {};
-            
+
             const allKeys = new Set([...cachedKeys, ...initialKeys]);
 
             // The only keys that should not be cleared are:
