@@ -6,9 +6,13 @@ import type {
66 Constrain ,
77 Register ,
88 TsrSerializable ,
9+ ValidateSerializableInput ,
910 Validator ,
1011} from '@tanstack/router-core'
11- import type { ConstrainValidator } from '../createServerFn'
12+ import type {
13+ ConstrainValidator ,
14+ ServerFnReturnType ,
15+ } from '../createServerFn'
1216
1317test ( 'createServerFn method with autocomplete' , ( ) => {
1418 createServerFn ( ) . handler ( ( options ) => {
@@ -308,7 +312,10 @@ test('createServerFn returns Date', () => {
308312 dates : [ new Date ( ) , new Date ( ) ] as const ,
309313 } ) )
310314
311- expectTypeOf ( fn ( ) ) . toEqualTypeOf < Promise < { dates : readonly [ Date , Date ] } > > ( )
315+ expectTypeOf < ReturnType < typeof fn > > ( ) . toMatchTypeOf < Promise < unknown > > ( )
316+ expectTypeOf < Awaited < ReturnType < typeof fn > > > ( ) . toMatchTypeOf <
317+ ValidateSerializableInput < Register , { dates : readonly [ Date , Date ] } >
318+ > ( )
312319} )
313320
314321test ( 'createServerFn returns undefined' , ( ) => {
@@ -379,6 +386,23 @@ describe('response', () => {
379386
380387 expectTypeOf ( fn ( ) ) . toEqualTypeOf < Promise < Response > > ( )
381388 } )
389+
390+ test ( `client receives union when handler may return Response or string` , ( ) => {
391+ const fn = createServerFn ( ) . handler ( ( ) => {
392+ const result : Response | 'Hello World' =
393+ Math . random ( ) > 0.5 ? new Response ( 'Hello World' ) : 'Hello World'
394+
395+ return result
396+ } )
397+
398+ expectTypeOf ( fn ( ) ) . toEqualTypeOf < Promise < Response | 'Hello World' > > ( )
399+ } )
400+ } )
401+
402+ test ( 'ServerFnReturnType distributes Response union' , ( ) => {
403+ expectTypeOf <
404+ ServerFnReturnType < Register , Response | 'Hello World' >
405+ > ( ) . toEqualTypeOf < Response | 'Hello World' > ( )
382406} )
383407
384408test ( 'createServerFn can be used as a mutation function' , ( ) => {
0 commit comments