@@ -377,26 +377,27 @@ describe('type tests', () => {
377377 } )
378378 } )
379379 describe ( 'endpoint schemas' , ( ) => {
380+ const argSchema = v . object ( { id : v . number ( ) } )
381+ const postSchema = v . object ( {
382+ id : v . number ( ) ,
383+ title : v . string ( ) ,
384+ body : v . string ( ) ,
385+ } ) satisfies v . GenericSchema < Post >
386+ const errorSchema = v . object ( {
387+ status : v . number ( ) ,
388+ data : v . unknown ( ) ,
389+ } ) satisfies v . GenericSchema < FetchBaseQueryError >
390+ const metaSchema = v . object ( {
391+ request : v . instance ( Request ) ,
392+ response : v . optional ( v . instance ( Response ) ) ,
393+ } ) satisfies v . GenericSchema < FetchBaseQueryMeta >
380394 test ( 'schemas must match' , ( ) => {
381- const postSchema = v . object ( {
382- id : v . number ( ) ,
383- title : v . string ( ) ,
384- body : v . string ( ) ,
385- } ) satisfies v . GenericSchema < Post >
386- const errorSchema = v . object ( {
387- status : v . number ( ) ,
388- data : v . unknown ( ) ,
389- } ) satisfies v . GenericSchema < FetchBaseQueryError >
390- const metaSchema = v . object ( {
391- request : v . instance ( Request ) ,
392- response : v . optional ( v . instance ( Response ) ) ,
393- } ) satisfies v . GenericSchema < FetchBaseQueryMeta >
394395 createApi ( {
395396 baseQuery : fetchBaseQuery ( { baseUrl : 'https://example.com' } ) ,
396397 endpoints : ( build ) => ( {
397398 query : build . query < Post , { id : number } > ( {
398399 query : ( { id } ) => `/post/${ id } ` ,
399- argSchema : v . object ( { id : v . number ( ) } ) ,
400+ argSchema,
400401 resultSchema : postSchema ,
401402 errorSchema,
402403 metaSchema,
@@ -465,6 +466,23 @@ describe('type tests', () => {
465466 } ) ,
466467 } )
467468 } )
469+ test ( 'schemas as a source of inference' , ( ) => {
470+ const api = createApi ( {
471+ baseQuery : fetchBaseQuery ( { baseUrl : 'https://example.com' } ) ,
472+ endpoints : ( build ) => ( {
473+ query : build . query ( {
474+ query : ( { id } ) => `/post/${ id } ` ,
475+ argSchema,
476+ resultSchema : postSchema ,
477+ } ) ,
478+ } ) ,
479+ } )
480+
481+ expectTypeOf ( api . endpoints . query . Types . QueryArg ) . toEqualTypeOf < {
482+ id : number
483+ } > ( )
484+ expectTypeOf ( api . endpoints . query . Types . ResultType ) . toEqualTypeOf < Post > ( )
485+ } )
468486 } )
469487 } )
470488} )
0 commit comments