@@ -9,6 +9,7 @@ import type {
9
9
Callback ,
10
10
Message ,
11
11
Maybe ,
12
+ Optionals ,
12
13
} from './types' ;
13
14
import ValidationError from './ValidationError' ;
14
15
import type Reference from './Reference' ;
@@ -27,6 +28,8 @@ import Schema, { SchemaInnerTypeDescription, SchemaSpec } from './schema';
27
28
import { ResolveOptions } from './Condition' ;
28
29
import parseJson from 'parse-json' ;
29
30
31
+ type InnerType < T > = T extends Array < infer I > ? I : never ;
32
+
30
33
export type RejectorFn = (
31
34
value : any ,
32
35
index : number ,
@@ -36,19 +39,18 @@ export type RejectorFn = (
36
39
export function create < C extends AnyObject = AnyObject , T = any > (
37
40
type ?: ISchema < T , C > ,
38
41
) {
39
- return new ArraySchema < T , C > ( type as any ) ;
42
+ return new ArraySchema < T [ ] | undefined , C > ( type as any ) ;
40
43
}
41
44
42
45
export default class ArraySchema <
43
- T ,
46
+ TIn extends any [ ] | null | undefined ,
44
47
TContext ,
45
48
TDefault = undefined ,
46
49
TFlags extends Flags = '' ,
47
- TIn extends any [ ] | null | undefined = T [ ] | undefined ,
48
50
> extends Schema < TIn , TContext , TDefault , TFlags > {
49
- innerType ?: ISchema < T , TContext > ;
51
+ readonly innerType ?: ISchema < InnerType < TIn > , TContext > ;
50
52
51
- constructor ( type ?: ISchema < T , TContext > ) {
53
+ constructor ( type ?: ISchema < InnerType < TIn > , TContext > ) {
52
54
super ( {
53
55
type : 'array' ,
54
56
check ( v : any ) : v is NonNullable < TIn > {
@@ -60,15 +62,13 @@ export default class ArraySchema<
60
62
this . innerType = type ;
61
63
}
62
64
63
- private get _subType ( ) {
64
- return this . innerType ;
65
- }
66
-
67
65
protected _cast ( _value : any , _opts : InternalOptions < TContext > ) {
68
66
const value = super . _cast ( _value , _opts ) ;
69
67
70
- //should ignore nulls here
71
- if ( ! this . _typeCheck ( value ) || ! this . innerType ) return value ;
68
+ // should ignore nulls here
69
+ if ( ! this . _typeCheck ( value ) || ! this . innerType ) {
70
+ return value ;
71
+ }
72
72
73
73
let isChanged = false ;
74
74
const castArray = value . map ( ( v , idx ) => {
@@ -151,6 +151,7 @@ export default class ArraySchema<
151
151
152
152
clone ( spec ?: SchemaSpec < any > ) {
153
153
const next = super . clone ( spec ) ;
154
+ // @ts -expect-error readonly
154
155
next . innerType = this . innerType ;
155
156
return next ;
156
157
}
@@ -160,22 +161,23 @@ export default class ArraySchema<
160
161
return this . transform ( parseJson ) ;
161
162
}
162
163
163
- concat < IT , IC , ID , IF extends Flags , IIn extends Maybe < IT [ ] > > (
164
- schema : ArraySchema < IT , IC , ID , IF , IIn > ,
164
+ concat < IIn extends Maybe < any [ ] > , IC , ID , IF extends Flags > (
165
+ schema : ArraySchema < IIn , IC , ID , IF > ,
165
166
) : ArraySchema <
166
- Concat < T , IT > ,
167
+ Concat < TIn , IIn > ,
167
168
TContext & IC ,
168
169
Extract < IF , 'd' > extends never ? TDefault : ID ,
169
- TFlags | IF ,
170
- Concat < TIn , IIn >
170
+ TFlags | IF
171
171
> ;
172
172
concat ( schema : this) : this;
173
173
concat ( schema : any ) : any {
174
174
let next = super . concat ( schema ) as this;
175
175
176
+ // @ts -expect-error readonly
176
177
next . innerType = this . innerType ;
177
178
178
179
if ( schema . innerType )
180
+ // @ts -expect-error readonly
179
181
next . innerType = next . innerType
180
182
? // @ts -expect-error Lazy doesn't have concat and will break
181
183
next . innerType . concat ( schema . innerType )
@@ -184,7 +186,9 @@ export default class ArraySchema<
184
186
return next ;
185
187
}
186
188
187
- of < U > ( schema : ISchema < U , TContext > ) : ArraySchema < U , TContext , TFlags > {
189
+ of < U > (
190
+ schema : ISchema < U , TContext > ,
191
+ ) : ArraySchema < U [ ] | Optionals < TIn > , TContext , TFlags > {
188
192
// FIXME: this should return a new instance of array without the default to be
189
193
let next = this . clone ( ) ;
190
194
@@ -194,8 +198,8 @@ export default class ArraySchema<
194
198
printValue ( schema ) ,
195
199
) ;
196
200
197
- // FIXME(ts):
198
- next . innerType = schema as any ;
201
+ // @ts -expect-error readonly
202
+ next . innerType = schema ;
199
203
200
204
return next as any ;
201
205
}
@@ -243,8 +247,6 @@ export default class ArraySchema<
243
247
} ) ;
244
248
}
245
249
246
- // ArraySchema<T, TContext, T[], SetFlag<TFlags, 'd'>, NonNullable<TIn>>
247
-
248
250
ensure ( ) {
249
251
return this . default < TIn > ( ( ) => [ ] as any ) . transform (
250
252
( val : TIn , original : any ) => {
@@ -285,35 +287,30 @@ export default class ArraySchema<
285
287
create . prototype = ArraySchema . prototype ;
286
288
287
289
export default interface ArraySchema <
288
- T ,
290
+ TIn extends any [ ] | null | undefined ,
289
291
TContext ,
290
292
TDefault = undefined ,
291
293
TFlags extends Flags = '' ,
292
- TIn extends any [ ] | null | undefined = T [ ] | undefined ,
293
294
> extends Schema < TIn , TContext , TDefault , TFlags > {
294
295
default < D extends Maybe < TIn > > (
295
296
def : Thunk < D > ,
296
- ) : ArraySchema < T , TContext , D , ToggleDefault < TFlags , D > , TIn > ;
297
+ ) : ArraySchema < TIn , TContext , D , ToggleDefault < TFlags , D > > ;
297
298
298
- defined (
299
- msg ?: Message ,
300
- ) : ArraySchema < T , TContext , TDefault , TFlags , Defined < TIn > > ;
301
- optional ( ) : ArraySchema < T , TContext , TDefault , TFlags , TIn | undefined > ;
299
+ defined ( msg ?: Message ) : ArraySchema < Defined < TIn > , TContext , TDefault , TFlags > ;
300
+ optional ( ) : ArraySchema < TIn | undefined , TContext , TDefault , TFlags > ;
302
301
303
302
required (
304
303
msg ?: Message ,
305
- ) : ArraySchema < T , TContext , TDefault , TFlags , NonNullable < TIn > > ;
306
- notRequired ( ) : ArraySchema < T , TContext , TDefault , TFlags , Maybe < TIn > > ;
304
+ ) : ArraySchema < NonNullable < TIn > , TContext , TDefault , TFlags > ;
305
+ notRequired ( ) : ArraySchema < Maybe < TIn > , TContext , TDefault , TFlags > ;
307
306
308
- nullable (
309
- msg ?: Message ,
310
- ) : ArraySchema < T , TContext , TDefault , TFlags , TIn | null > ;
311
- nonNullable ( ) : ArraySchema < T , TContext , TDefault , TFlags , NotNull < TIn > > ;
307
+ nullable ( msg ?: Message ) : ArraySchema < TIn | null , TContext , TDefault , TFlags > ;
308
+ nonNullable ( ) : ArraySchema < NotNull < TIn > , TContext , TDefault , TFlags > ;
312
309
313
310
strip (
314
311
enabled : false ,
315
- ) : ArraySchema < T , TContext , TDefault , UnsetFlag < TFlags , 's' > > ;
312
+ ) : ArraySchema < TIn , TContext , TDefault , UnsetFlag < TFlags , 's' > > ;
316
313
strip (
317
314
enabled ?: true ,
318
- ) : ArraySchema < T , TContext , TDefault , SetFlag < TFlags , 's' > > ;
315
+ ) : ArraySchema < TIn , TContext , TDefault , SetFlag < TFlags , 's' > > ;
319
316
}
0 commit comments