@@ -4,11 +4,12 @@ import { type _PromiseMerged } from './utils'
4
4
import { type NavigationResult } from './navigation-guard'
5
5
import type { RouteLocationNormalizedLoaded , Router } from 'vue-router'
6
6
import { type _Awaitable } from '../utils'
7
+ import { ErrorDefault } from './types-config'
7
8
8
9
/**
9
10
* Base type for a data loader entry. Each Data Loader has its own entry in the `loaderEntries` (accessible via `[LOADER_ENTRIES_KEY]`) map.
10
11
*/
11
- export interface DataLoaderEntryBase < Data = unknown > {
12
+ export interface DataLoaderEntryBase < Data = unknown , TError = unknown > {
12
13
/**
13
14
* Data stored in the entry.
14
15
*/
@@ -17,7 +18,7 @@ export interface DataLoaderEntryBase<Data = unknown> {
17
18
/**
18
19
* Error if there was an error.
19
20
*/
20
- error : ShallowRef < any > // any is simply more convenient for errors
21
+ error : ShallowRef < TError | null > // any is simply more convenient for errors
21
22
22
23
/**
23
24
* Location the data was loaded for or `null` if the data is not loaded.
@@ -56,7 +57,7 @@ export interface DataLoaderEntryBase<Data = unknown> {
56
57
* Error that was staged by a loader. This is used to avoid showing the old error while the new data is loading.
57
58
* Calling the internal `commit()` function will replace the error with the staged error.
58
59
*/
59
- stagedError : any | null
60
+ stagedError : TError | null
60
61
61
62
// entry instance
62
63
@@ -127,7 +128,7 @@ export interface _DefineDataLoaderOptionsBase_Common {
127
128
* Options for a data loader that returns a data that is possibly `undefined`. Available for data loaders
128
129
* implementations so they can be used in `defineLoader()` overloads.
129
130
*/
130
- export interface DefineDataLoaderOptionsBase_LaxData
131
+ export interface DefineDataLoaderOptionsBase_LaxData < TError = any >
131
132
extends _DefineDataLoaderOptionsBase_Common {
132
133
lazy ?:
133
134
| boolean
@@ -142,10 +143,36 @@ export interface DefineDataLoaderOptionsBase_LaxData
142
143
143
144
errors ?:
144
145
| boolean
145
- | Array < new ( ...args : any ) => any >
146
- | ( ( reason ?: unknown ) => boolean )
146
+ // NOTE:
147
+ // | (any extends TError ? any[] : readonly (new (...args: any[]) => TError)[])
148
+ | Array < new ( ...args : any [ ] ) => TError >
149
+ | ( any extends TError
150
+ ? ( reason ?: unknown ) => boolean
151
+ : ( reason ?: unknown ) => reason is TError )
147
152
}
148
153
154
+ export function errorsFromArray <
155
+ const T extends readonly ( new ( ...args : any ) => any ) [ ] ,
156
+ > (
157
+ errorConstructorsArray : T
158
+ ) : ( reason ?: unknown ) => reason is _UnionFromConstructorsArray < T > {
159
+ return ( r : unknown ) : r is _UnionFromConstructorsArray < T > =>
160
+ errorConstructorsArray . some ( ( ErrConstructor ) => r instanceof ErrConstructor )
161
+ }
162
+
163
+ /**
164
+ * Extracts the union of the constructors from an array of constructors.
165
+ * @internal
166
+ */
167
+ export type _UnionFromConstructorsArray < T extends readonly any [ ] > = T extends readonly [
168
+ new ( ...args : any [ ] ) => infer R ,
169
+ ...infer Rest ,
170
+ ]
171
+ ? Rest extends readonly [ any , ...any [ ] ]
172
+ ? R | _UnionFromConstructorsArray < Rest >
173
+ : R
174
+ : never
175
+
149
176
/**
150
177
* Options for a data loader making the data defined without it being possibly `undefined`. Available for data loaders
151
178
* implementations so they can be used in `defineLoader()` overloads.
@@ -192,7 +219,7 @@ export interface DefineDataLoader<Context extends DataLoaderContextBase> {
192
219
* Data Loader composable returned by `defineLoader()`.
193
220
* @see {@link DefineDataLoader }
194
221
*/
195
- export interface UseDataLoader < Data = unknown > {
222
+ export interface UseDataLoader < Data = unknown , TError = unknown > {
196
223
[ IS_USE_DATA_LOADER_KEY ] : true
197
224
198
225
/**
@@ -222,7 +249,7 @@ export interface UseDataLoader<Data = unknown> {
222
249
// excluding `undefined` allows to await for lazy loaders and others
223
250
Exclude < Data , NavigationResult | undefined > ,
224
251
// or use it as a composable
225
- UseDataLoaderResult < Exclude < Data , NavigationResult > >
252
+ UseDataLoaderResult < Exclude < Data , NavigationResult > , TError >
226
253
>
227
254
228
255
/**
@@ -236,7 +263,7 @@ export interface UseDataLoader<Data = unknown> {
236
263
* Internal properties of a data loader composable. Used by the internal implementation of `defineLoader()`. **Should
237
264
* not be used in application code.**
238
265
*/
239
- export interface UseDataLoaderInternals < Data = unknown > {
266
+ export interface UseDataLoaderInternals < Data = unknown , TError = unknown > {
240
267
/**
241
268
* Loads the data from the cache if possible, otherwise loads it from the loader and awaits it.
242
269
*
@@ -263,13 +290,13 @@ export interface UseDataLoaderInternals<Data = unknown> {
263
290
*
264
291
* @param router - router instance
265
292
*/
266
- getEntry ( router : Router ) : DataLoaderEntryBase < Data >
293
+ getEntry ( router : Router ) : DataLoaderEntryBase < Data , TError >
267
294
}
268
295
269
296
/**
270
297
* Return value of a loader composable defined with `defineLoader()`.
271
298
*/
272
- export interface UseDataLoaderResult < Data = unknown > {
299
+ export interface UseDataLoaderResult < Data = unknown , TError = ErrorDefault > {
273
300
/**
274
301
* Data returned by the loader. If the data loader is lazy, it will be undefined until the first load.
275
302
*/
@@ -283,7 +310,7 @@ export interface UseDataLoaderResult<Data = unknown> {
283
310
/**
284
311
* Error if there was an error.
285
312
*/
286
- error : ShallowRef < any > // any is simply more convenient for errors
313
+ error : ShallowRef < TError | null > // any is simply more convenient for errors
287
314
288
315
/**
289
316
* Reload the data using the current route location. Returns a promise that resolves when the data is reloaded. This
0 commit comments