()
| 43 | |
| 44 | // @ts-expect-error |
| 45 | function Page() { |
| 46 | // unspecified query function should default to unknown |
| 47 | const noQueryFn = useQuery(() => ({ queryKey: key })) |
| 48 | expectTypeOf(noQueryFn.data).toEqualTypeOf<unknown>() |
| 49 | expectTypeOf(noQueryFn.error).toEqualTypeOf<Error | null>() |
| 50 | |
| 51 | // it should infer the result type from the query function |
| 52 | const fromQueryFn = useQuery(() => ({ |
| 53 | queryKey: key, |
| 54 | queryFn: () => 'test', |
| 55 | })) |
| 56 | expectTypeOf(fromQueryFn.data).toEqualTypeOf<string | undefined>() |
| 57 | expectTypeOf(fromQueryFn.error).toEqualTypeOf<Error | null>() |
| 58 | |
| 59 | // it should be possible to specify the result type |
| 60 | const withResult = useQuery<string>(() => ({ |
| 61 | queryKey: key, |
| 62 | queryFn: () => 'test', |
| 63 | })) |
| 64 | expectTypeOf(withResult.data).toEqualTypeOf<string | undefined>() |
| 65 | expectTypeOf(withResult.error).toEqualTypeOf<Error | null>() |
| 66 | |
| 67 | // it should be possible to specify the error type |
| 68 | const withError = useQuery<string, Error>(() => ({ |
| 69 | queryKey: key, |
| 70 | queryFn: () => 'test', |
| 71 | })) |
| 72 | expectTypeOf(withError.data).toEqualTypeOf<string | undefined>() |
| 73 | expectTypeOf(withError.error).toEqualTypeOf<Error | null>() |
| 74 | |
| 75 | // it should provide the result type in the configuration |
| 76 | useQuery(() => ({ |
| 77 | queryKey: [key], |
| 78 | queryFn: () => true, |
| 79 | })) |
| 80 | |
| 81 | // it should be possible to specify a union type as result type |
| 82 | const unionTypeSync = useQuery(() => ({ |
| 83 | queryKey: key, |
| 84 | queryFn: () => (Math.random() > 0.5 ? ('a' as const) : ('b' as const)), |
| 85 | })) |
| 86 | expectTypeOf(unionTypeSync.data).toEqualTypeOf<'a' | 'b' | undefined>() |
| 87 | const unionTypeAsync = useQuery<'a' | 'b'>(() => ({ |
| 88 | queryKey: key, |
| 89 | queryFn: () => Promise.resolve(Math.random() > 0.5 ? 'a' : 'b'), |
| 90 | })) |
| 91 | expectTypeOf(unionTypeAsync.data).toEqualTypeOf<'a' | 'b' | undefined>() |
| 92 | |
| 93 | // should error when the query function result does not match with the specified type |
| 94 | // @ts-expect-error |
| 95 | useQuery<number>(() => ({ queryKey: key, queryFn: () => 'test' })) |
| 96 | |
| 97 | // it should infer the result type from a generic query function |
| 98 | function queryFn<T = string>(): Promise<T> { |
| 99 | return Promise.resolve({} as T) |
| 100 | } |
| 101 | |
| 102 | const fromGenericQueryFn = useQuery(() => ({ |
nothing calls this directly
no test coverage detected
searching dependent graphs…