@@ -409,11 +409,11 @@ test("codec type enforcement - correct encode/decode signatures", () => {
409
409
encode : ( value : number ) => String ( value ) , // core.input<B> -> core.output<A>
410
410
} ) ;
411
411
412
- // These should compile without errors - correct types
413
- expectTypeOf < ( value : string , payload : z . core . ParsePayload < string > ) => number > (
412
+ // These should compile without errors - correct types (async support)
413
+ expectTypeOf < ( value : string , payload : z . core . ParsePayload < string > ) => z . core . util . MaybeAsync < number > > (
414
414
stringToNumberCodec . def . transform
415
415
) . toBeFunction ( ) ;
416
- expectTypeOf < ( value : number , payload : z . core . ParsePayload < number > ) => string > (
416
+ expectTypeOf < ( value : number , payload : z . core . ParsePayload < number > ) => z . core . util . MaybeAsync < string > > (
417
417
stringToNumberCodec . def . reverseTransform
418
418
) . toBeFunction ( ) ;
419
419
@@ -450,6 +450,36 @@ test("codec type enforcement - correct encode/decode signatures", () => {
450
450
} ) ;
451
451
} ) ;
452
452
453
+ test ( "async codec functionality" , async ( ) => {
454
+ // Test that async encode/decode functions work properly
455
+ const asyncCodec = z . codec ( z . string ( ) , z . number ( ) , {
456
+ decode : async ( str ) => {
457
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ; // Simulate async work
458
+ return Number . parseFloat ( str ) ;
459
+ } ,
460
+ encode : async ( num ) => {
461
+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1 ) ) ; // Simulate async work
462
+ return num . toString ( ) ;
463
+ } ,
464
+ } ) ;
465
+
466
+ // Test async decode/encode
467
+ const decoded = await z . decodeAsync ( asyncCodec , "42.5" ) ;
468
+ expect ( decoded ) . toBe ( 42.5 ) ;
469
+
470
+ const encoded = await z . encodeAsync ( asyncCodec , 42.5 ) ;
471
+ expect ( encoded ) . toBe ( "42.5" ) ;
472
+
473
+ // Test that both sync and async work
474
+ const mixedCodec = z . codec ( z . string ( ) , z . number ( ) , {
475
+ decode : async ( str ) => Number . parseFloat ( str ) ,
476
+ encode : ( num ) => num . toString ( ) , // sync encode
477
+ } ) ;
478
+
479
+ const mixedResult = await z . decodeAsync ( mixedCodec , "123" ) ;
480
+ expect ( mixedResult ) . toBe ( 123 ) ;
481
+ } ) ;
482
+
453
483
test ( "codec type enforcement - complex types" , ( ) => {
454
484
type User = { id : number ; name : string } ;
455
485
type UserInput = { id : string ; name : string } ;
@@ -463,11 +493,11 @@ test("codec type enforcement - complex types", () => {
463
493
}
464
494
) ;
465
495
466
- // Verify correct types are inferred
467
- expectTypeOf < ( input : UserInput , payload : z . core . ParsePayload < UserInput > ) => User > (
496
+ // Verify correct types are inferred (async support)
497
+ expectTypeOf < ( input : UserInput , payload : z . core . ParsePayload < UserInput > ) => z . core . util . MaybeAsync < User > > (
468
498
userCodec . def . transform
469
499
) . toBeFunction ( ) ;
470
- expectTypeOf < ( user : User , payload : z . core . ParsePayload < User > ) => UserInput > (
500
+ expectTypeOf < ( user : User , payload : z . core . ParsePayload < User > ) => z . core . util . MaybeAsync < UserInput > > (
471
501
userCodec . def . reverseTransform
472
502
) . toBeFunction ( ) ;
473
503
0 commit comments