Repository: tannerlinsley/react-query
Pull request #2341 (2021-06-04 09:07:45)

chore: turn on noUncheckedIndexedAccess compiler flag
only changes were needed in tests, but it will make sure we don't rely on unsafe index accesses on objects and / or arrays in the future.

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/tannerlinsley/react-query/3ZSWBHTGKy8arC9TYAo6EvR8aKAX](https://vercel.com/tannerlinsley/react-query/3ZSWBHTGKy8arC9TYAo6EvR8aKAX)  
✅ Preview: *In Progress*


codesandbox[bot]: This pull request is automatically built and testable in [CodeSandbox](https://codesandbox.io).

  To see build info of the built libraries, click [here](https://ci.codesandbox.io/status/tannerlinsley/react-query/pr/2341/builds/136881) or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f9fab4f2ee001cb2c4ad95fb23efdb4110d8c415:

|Sandbox| Source |
|--|--|
|[tannerlinsley/react-query: basic](https://codesandbox.io/s/tannerlinsleyreact-query-basic-n6own)| Configuration |
|[tannerlinsley/react-query: basic-typescript](https://codesandbox.io/s/tannerlinsleyreact-query-basic-typescript-r4942)| Configuration |


Patch:
diff --git a/docs/src/pages/guides/infinite-queries.md b/docs/src/pages/guides/infinite-queries.md
index 870dbfc6d..7981d3c20 100644
--- a/docs/src/pages/guides/infinite-queries.md
+++ b/docs/src/pages/guides/infinite-queries.md
@@ -158,11 +158,10 @@ queryClient.setQueryData('projects', data => ({
 Manually removing a single value from an individual page:
 
 ```js
-const newPagesArray = []
-oldPagesArray?.pages.forEach(page => {
-  const newData = page.filter(val => val.id !== updatedId)
-  newPagesArray.push(newData)
-})
+const newPagesArray = oldPagesArray?.pages.map((page) =>
+  page.filter((val) => val.id !== updatedId)
+) ?? []
+
 queryClient.setQueryData('projects', data => ({
   pages: newPagesArray,
   pageParams: data.pageParams,
diff --git a/tsconfig.json b/tsconfig.json
index cf0b28838..c88aaec9b 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -11,6 +11,7 @@
     "noImplicitThis": true,
     "noUnusedLocals": true,
     "noUnusedParameters": true,
+    "noUncheckedIndexedAccess": true,
     "skipLibCheck": true,
     "strict": true,
     "types": ["jest"],


Test patch:
diff --git a/src/core/tests/queryClient.test.tsx b/src/core/tests/queryClient.test.tsx
index 0dca395e2..0a62a0148 100644
--- a/src/core/tests/queryClient.test.tsx
+++ b/src/core/tests/queryClient.test.tsx
@@ -344,7 +344,7 @@ describe('queryClient', () => {
       const data = {
         pages: ['data'],
         pageParams: [undefined],
-      }
+      } as const
 
       const fetchFn: QueryFunction<StrictData, StrictQueryKey> = () =>
         Promise.resolve(data.pages[0])
diff --git a/src/core/tests/utils.test.tsx b/src/core/tests/utils.test.tsx
index 9f1f5411b..189f339ea 100644
--- a/src/core/tests/utils.test.tsx
+++ b/src/core/tests/utils.test.tsx
@@ -295,10 +295,10 @@ describe('core/utils', () => {
       expect(result.todos).not.toBe(next.todos)
       expect(result.todos[0]).not.toBe(prev.todos[0])
       expect(result.todos[0]).not.toBe(next.todos[0])
-      expect(result.todos[0].id).toBe(next.todos[0].id)
-      expect(result.todos[0].meta).toBe(prev.todos[0].meta)
-      expect(result.todos[0].state).not.toBe(next.todos[0].state)
-      expect(result.todos[0].state.done).toBe(next.todos[0].state.done)
+      expect(result.todos[0]?.id).toBe(next.todos[0]?.id)
+      expect(result.todos[0]?.meta).toBe(prev.todos[0]?.meta)
+      expect(result.todos[0]?.state).not.toBe(next.todos[0]?.state)
+      expect(result.todos[0]?.state.done).toBe(next.todos[0]?.state.done)
       expect(result.todos[1]).toBe(prev.todos[1])
     })
   })
diff --git a/src/react/tests/useQuery.test.tsx b/src/react/tests/useQuery.test.tsx
index 3c752b7e3..7e94f0209 100644
--- a/src/react/tests/useQuery.test.tsx
+++ b/src/react/tests/useQuery.test.tsx
@@ -818,7 +818,7 @@ describe('useQuery', () => {
     expect(states[0]).toMatchObject({ data: undefined, isFetching: true })
     expect(states[1]).toMatchObject({ data: 'test', isFetching: false })
     expect(states[2]).toMatchObject({ data: 'test', isFetching: false })
-    expect(states[1].dataUpdatedAt).not.toBe(states[2].dataUpdatedAt)
+    expect(states[1]?.dataUpdatedAt).not.toBe(states[2]?.dataUpdatedAt)
   })
 
   it('should track properties and only re-render when a tracked property changes', async () => {
@@ -1039,13 +1039,13 @@ describe('useQuery', () => {
 
     await waitFor(() => expect(states.length).toBe(4))
 
-    const todos = states[2].data!
-    const todo1 = todos[0]
-    const todo2 = todos[1]
+    const todos = states[2]?.data
+    const todo1 = todos?.[0]
+    const todo2 = todos?.[1]
 
-    const newTodos = states[3].data!
-    const newTodo1 = newTodos[0]
-    const newTodo2 = newTodos[1]
+    const newTodos = states[3]?.data
+    const newTodo1 = newTodos?.[0]
+    const newTodo2 = newTodos?.[1]
 
     expect(todos).toEqual(result1)
     expect(newTodos).toEqual(result2)
@@ -1465,7 +1465,7 @@ describe('useQuery', () => {
       status: 'error',
       isPreviousData: false,
     })
-    expect(states[7].error).toHaveProperty('message', 'Error test')
+    expect(states[7]?.error).toHaveProperty('message', 'Error test')
 
     consoleMock.mockRestore()
   })
@@ -2304,7 +2304,7 @@ describe('useQuery', () => {
 
     await sleep(20)
 
-    expect(states[1].data).toEqual([key, variables])
+    expect(states[1]?.data).toEqual([key, variables])
   })
 
   it('should not refetch query on focus when `enabled` is set to `false`', async () => {