Skip to content

Commit 184cbb0

Browse files
Fix ReqBase<TData>['error'] type (#334)
* Fix ReqBase<TData>['error'] type It is currently marked as always having a value. But this is not true, its value is `undefined` when the request is either loading or successful. This means that checking whether the `error` property has a value will trigger `@typescript-eslint/strict-boolean-expressions` ESLint rule doc: https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/strict-boolean-expressions.md Example playground: https://www.typescriptlang.org/play?#code/JYWwDg9gTgLgBAVwM4FMBiKYGMAWcBmUEIcA5MigLQ4wxikBQDWEAdkvAN5wA2EAhgBNgrAOYAaOCihEokwfxj84AXzgBeRKgzYcAClI06SAFwB6MygAe-cDxQA6FiDP8wwUpM4rJAbQC6AJRMwPhwetKygXCcDHDxcCzsEPYOfKIGACo4KHAwAJ5gKEhwSPz5eTjAJUnCMMBscADuwDw8cPw8TeUlAEa5MFAIKKTBKgxAA * Fix ArrayDestructure types in useMutation and useQuery. * Update tests. Co-authored-by: Zebulan Stanphill <[email protected]>
1 parent 7d33349 commit 184cbb0

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

src/__tests__/useFetch.test.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe('useFetch - responseType', (): void => {
168168
expect(result.current.loading).toBe(true)
169169
await waitForNextUpdate()
170170
expect(result.current.loading).toBe(false)
171-
expect(result.current.error.name).toBe('FetchError')
171+
expect(result.current.error?.name).toBe('FetchError')
172172
})
173173

174174
it('should process .text() with default array responseType', async (): Promise<void> => {
@@ -353,8 +353,8 @@ describe('timeouts', (): void => {
353353
expect(result.current.loading).toBe(true)
354354
await waitForNextUpdate()
355355
expect(result.current.loading).toBe(false)
356-
expect(result.current.error.name).toBe('AbortError')
357-
expect(result.current.error.message).toBe('Timeout Error')
356+
expect(result.current.error?.name).toBe('AbortError')
357+
expect(result.current.error?.message).toBe('Timeout Error')
358358
expect(onAbort).toBeCalled()
359359
expect(onTimeout).toBeCalled()
360360
expect(onAbort).toHaveBeenCalledTimes(1)
@@ -386,8 +386,8 @@ describe('timeouts', (): void => {
386386
expect(fetch.mock.calls[0][0]).toBe('https://example.com/todos')
387387
expect(fetch).toHaveBeenCalledTimes(2)
388388
expect(result.current.loading).toBe(false)
389-
expect(result.current.error.name).toBe('AbortError')
390-
expect(result.current.error.message).toBe('Timeout Error')
389+
expect(result.current.error?.name).toBe('AbortError')
390+
expect(result.current.error?.message).toBe('Timeout Error')
391391
expect(onAbort).toBeCalled()
392392
expect(onTimeout).toBeCalled()
393393
expect(onAbort).toHaveBeenCalledTimes(2)

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export interface Data<TData> {
133133
export interface ReqBase<TData> {
134134
data: TData | undefined
135135
loading: boolean
136-
error: Error
136+
error: Error | undefined
137137
cache: Cache
138138
}
139139

src/useMutation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { invariant, isString, useURLRequiredInvariant } from './utils'
77
type ArrayDestructure<TData = any> = [
88
TData | undefined,
99
boolean,
10-
Error,
10+
Error | undefined,
1111
(variables?: object) => Promise<any>,
1212
]
1313
interface ObjectDestructure<TData = any> extends ReqBase<TData> {

src/useQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { invariant, isString, useURLRequiredInvariant } from './utils'
77
type ArrayDestructure<TData = any> = [
88
TData | undefined,
99
boolean,
10-
Error,
10+
Error | undefined,
1111
(variables?: object) => Promise<any>,
1212
]
1313
interface ObjectDestructure<TData = any> extends ReqBase<TData> {

0 commit comments

Comments
 (0)