1+ /* eslint-disable @typescript-eslint/no-explicit-any */
12import { derived , get , type Readable , type Updater , type Writable } from 'svelte/store' ;
23import type { InputConstraint } from '../index.js' ;
34import { SuperFormError } from '$lib/errors.js' ;
45import { pathExists , traversePath } from '../traversal.js' ;
56import { splitPath , type FormPath , type FormPathLeaves , type FormPathType } from '../stringPath.js' ;
67import type { FormPathArrays } from '../stringPath.js' ;
78import type { SuperForm , TaintOption } from './index.js' ;
8- import type { Prettify } from '$lib/utils.js' ;
9+ import type { IsAny , Prettify } from '$lib/utils.js' ;
910
1011export type ProxyOptions = {
1112 taint ?: TaintOption ;
1213} ;
1314
14- type FormPaths < T extends Record < string , unknown > > = FormPath < T > | FormPathLeaves < T > ;
15+ type FormPaths < T extends Record < string , unknown > , Type = any > =
16+ | FormPath < T , Type >
17+ | FormPathLeaves < T , Type > ;
1518
1619type CorrectProxyType < In , Out , T extends Record < string , unknown > , Path extends FormPaths < T > > =
1720 NonNullable < FormPathType < T , Path > > extends In ? Writable < Out > : never ;
1821
22+ type PathType < Type , T , Path extends string > =
23+ IsAny < Type > extends true ? FormPathType < T , Path > : Type ;
24+
1925type DefaultOptions = {
2026 trueStringValue : string ;
2127 dateFormat :
@@ -236,27 +242,25 @@ function _stringProxy<T extends Record<string, unknown>, Path extends FormPaths<
236242 } ;
237243}
238244
239- // eslint-disable-next-line @typescript-eslint/no-explicit-any
240245type ValueErrors = any [ ] ;
241246
242247export type ArrayProxy < T , Path = string , Errors = ValueErrors > = {
243248 path : Path ;
244- values : Writable < T & unknown [ ] > ;
249+ values : Writable < T [ ] & unknown [ ] > ;
245250 errors : Writable < string [ ] | undefined > ;
246251 valueErrors : Writable < Errors > ;
247252} ;
248253
249- export function arrayProxy < T extends Record < string , unknown > , Path extends FormPathArrays < T > > (
250- // eslint-disable-next-line @typescript-eslint/no-explicit-any
254+ export function arrayProxy <
255+ T extends Record < string , unknown > ,
256+ Path extends FormPathArrays < T , ArrType > ,
257+ ArrType = any
258+ > (
251259 superForm : SuperForm < T , any > ,
252260 path : Path ,
253261 options ?: { taint ?: TaintOption }
254- ) : ArrayProxy < FormPathType < T , Path > , Path > {
255- const formErrors = fieldProxy (
256- superForm . errors ,
257- // eslint-disable-next-line @typescript-eslint/no-explicit-any
258- `${ path } ` as any
259- ) ;
262+ ) : ArrayProxy < FormPathType < T , Path > extends ( infer U ) [ ] ? U : never , Path > {
263+ const formErrors = fieldProxy ( superForm . errors , `${ path } ` as any ) ;
260264
261265 const onlyFieldErrors = derived < typeof formErrors , ValueErrors > ( formErrors , ( $errors ) => {
262266 const output : ValueErrors = [ ] ;
@@ -320,13 +324,10 @@ export function arrayProxy<T extends Record<string, unknown>, Path extends FormP
320324
321325 return {
322326 path,
323- // eslint-disable-next-line @typescript-eslint/no-explicit-any
324327 values : values as any ,
325- errors : fieldProxy (
326- superForm . errors ,
327- // eslint-disable-next-line @typescript-eslint/no-explicit-any
328- `${ path } ._errors` as any
329- ) as Writable < string [ ] | undefined > ,
328+ errors : fieldProxy ( superForm . errors , `${ path } ._errors` as any ) as Writable <
329+ string [ ] | undefined
330+ > ,
330331 valueErrors : fieldErrors
331332 } ;
332333}
@@ -339,12 +340,15 @@ export type FormFieldProxy<T, Path = string> = {
339340 tainted : Writable < boolean | undefined > ;
340341} ;
341342
342- export function formFieldProxy < T extends Record < string , unknown > , Path extends FormPathLeaves < T > > (
343- // eslint-disable-next-line @typescript-eslint/no-explicit-any
343+ export function formFieldProxy <
344+ T extends Record < string , unknown > ,
345+ Path extends FormPathLeaves < T , Type > ,
346+ Type = any
347+ > (
344348 superForm : SuperForm < T , any > ,
345349 path : Path ,
346350 options ?: ProxyOptions
347- ) : FormFieldProxy < FormPathType < T , Path > , Path > {
351+ ) : FormFieldProxy < PathType < Type , T , Path > , Path > {
348352 const path2 = splitPath ( path ) ;
349353 // Filter out array indices, the constraints structure doesn't contain these.
350354 const constraintsPath = path2 . filter ( ( p ) => / \D / . test ( String ( p ) ) ) . join ( '.' ) ;
@@ -387,7 +391,6 @@ export function formFieldProxy<T extends Record<string, unknown>, Path extends F
387391 return {
388392 path,
389393 value : superFieldProxy ( superForm , path , options ) ,
390- // eslint-disable-next-line @typescript-eslint/no-explicit-any
391394 errors : fieldProxy ( superForm . errors , path as any ) as unknown as Writable < string [ ] | undefined > ,
392395 constraints : fieldProxy ( superForm . constraints , constraintsPath as never ) as Writable <
393396 InputConstraint | undefined
@@ -396,10 +399,10 @@ export function formFieldProxy<T extends Record<string, unknown>, Path extends F
396399 } ;
397400}
398401
399- function updateProxyField < T extends Record < string , unknown > , Path extends string > (
402+ function updateProxyField < T extends Record < string , unknown > , Path extends string , Type = any > (
400403 obj : T ,
401404 path : ( string | number | symbol ) [ ] ,
402- updater : Updater < FormPathType < T , Path > >
405+ updater : Updater < PathType < Type , T , Path > >
403406) {
404407 const output = traversePath ( obj , path , ( { parent, key, value } ) => {
405408 if ( value === undefined ) parent [ key ] = / \D / . test ( key ) ? { } : [ ] ;
@@ -418,11 +421,11 @@ type SuperFieldProxy<T> = {
418421 update ( this : void , updater : Updater < T > , options ?: { taint ?: TaintOption } ) : void ;
419422} ;
420423
421- function superFieldProxy < T extends Record < string , unknown > , Path extends string > (
424+ function superFieldProxy < T extends Record < string , unknown > , Path extends string , Type = any > (
422425 superForm : SuperForm < T > ,
423426 path : Path ,
424427 baseOptions ?: ProxyOptions
425- ) : SuperFieldProxy < FormPathType < T , Path > > {
428+ ) : SuperFieldProxy < PathType < Type , T , Path > > {
426429 const form = superForm . form ;
427430 const path2 = splitPath ( path ) ;
428431
@@ -436,10 +439,10 @@ function superFieldProxy<T extends Record<string, unknown>, Path extends string>
436439 const unsub = proxy . subscribe ( ...params ) ;
437440 return ( ) => unsub ( ) ;
438441 } ,
439- update ( upd : Updater < FormPathType < T , Path > > , options ?: ProxyOptions ) {
442+ update ( upd : Updater < PathType < Type , T , Path > > , options ?: ProxyOptions ) {
440443 form . update ( ( data ) => updateProxyField ( data , path2 , upd ) , options ?? baseOptions ) ;
441444 } ,
442- set ( value : FormPathType < T , Path > , options ?: ProxyOptions ) {
445+ set ( value : PathType < Type , T , Path > , options ?: ProxyOptions ) {
443446 form . update ( ( data ) => updateProxyField ( data , path2 , ( ) => value ) , options ?? baseOptions ) ;
444447 }
445448 } ;
@@ -462,11 +465,15 @@ function isSuperForm<T extends Record<string, unknown>>(
462465
463466export type FieldProxy < T > = Writable < T > ;
464467
465- export function fieldProxy < T extends Record < string , unknown > , Path extends FormPaths < T > > (
468+ export function fieldProxy <
469+ T extends Record < string , unknown > ,
470+ Path extends FormPaths < T , Type > ,
471+ Type = any
472+ > (
466473 form : Writable < T > | SuperForm < T , unknown > ,
467474 path : Path ,
468475 options ?: ProxyOptions
469- ) : FieldProxy < FormPathType < T , Path > > {
476+ ) : FieldProxy < PathType < Type , T , Path > > {
470477 const path2 = splitPath ( path ) ;
471478
472479 if ( isSuperForm ( form , options ) ) {
@@ -483,10 +490,10 @@ export function fieldProxy<T extends Record<string, unknown>, Path extends FormP
483490 const unsub = proxy . subscribe ( ...params ) ;
484491 return ( ) => unsub ( ) ;
485492 } ,
486- update ( upd : Updater < FormPathType < T , Path > > ) {
493+ update ( upd : Updater < PathType < Type , T , Path > > ) {
487494 form . update ( ( data ) => updateProxyField ( data , path2 , upd ) ) ;
488495 } ,
489- set ( value : FormPathType < T , Path > ) {
496+ set ( value : PathType < Type , T , Path > ) {
490497 form . update ( ( data ) => updateProxyField ( data , path2 , ( ) => value ) ) ;
491498 }
492499 } ;
0 commit comments