1+ import type { AsyncThunkDispatchConfig } from '@internal/createAsyncThunk'
12import type { TSVersion } from '@phryneas/ts-version'
23import type {
34 AsyncThunk ,
@@ -14,9 +15,11 @@ import {
1415} from '@reduxjs/toolkit'
1516import type { AxiosError } from 'axios'
1617import apiRequest from 'axios'
17- import type { AsyncThunkDispatchConfig } from '@internal/createAsyncThunk '
18+ import type { AnyFunction , AnyNonNullishValue } from '../tsHelpers '
1819
19- const defaultDispatch = ( ( ) => { } ) as ThunkDispatch < { } , any , UnknownAction >
20+ const defaultDispatch = ( ( ) => {
21+ /* No-Op */
22+ } ) as ThunkDispatch < AnyNonNullishValue , any , UnknownAction >
2023const unknownAction = { type : 'foo' } as UnknownAction
2124
2225describe ( 'type tests' , ( ) => {
@@ -96,19 +99,17 @@ describe('type tests', () => {
9699 { id : 'a' , title : 'First' } ,
97100 ]
98101
99- const correctDispatch = ( ( ) => { } ) as ThunkDispatch <
100- BookModel [ ] ,
101- { userAPI : Function } ,
102- UnknownAction
103- >
102+ const correctDispatch = ( ( ) => {
103+ /* No-Op */
104+ } ) as ThunkDispatch < BookModel [ ] , { userAPI : AnyFunction } , UnknownAction >
104105
105106 // Verify that the the first type args to createAsyncThunk line up right
106107 const fetchBooksTAC = createAsyncThunk <
107108 BookModel [ ] ,
108109 number ,
109110 {
110111 state : BooksState
111- extra : { userAPI : Function }
112+ extra : { userAPI : AnyFunction }
112113 }
113114 > (
114115 'books/fetch' ,
@@ -119,7 +120,7 @@ describe('type tests', () => {
119120
120121 expectTypeOf ( state ) . toEqualTypeOf < BookModel [ ] > ( )
121122
122- expectTypeOf ( extra ) . toEqualTypeOf < { userAPI : Function } > ( )
123+ expectTypeOf ( extra ) . toEqualTypeOf < { userAPI : AnyFunction } > ( )
123124
124125 return fakeBooks
125126 } ,
@@ -160,7 +161,7 @@ describe('type tests', () => {
160161
161162 test ( 'regression #1156: union return values fall back to allowing only single member' , ( ) => {
162163 const fn = createAsyncThunk ( 'session/isAdmin' , async ( ) => {
163- const response : boolean = false
164+ const response = false
164165 return response
165166 } )
166167 } )
@@ -493,15 +494,19 @@ describe('type tests', () => {
493494 return 'ret' as const
494495 } )
495496
496- expectTypeOf ( thunk ) . toEqualTypeOf < AsyncThunk < 'ret' , void , { } > > ( )
497+ expectTypeOf ( thunk ) . toEqualTypeOf <
498+ AsyncThunk < 'ret' , void , AnyNonNullishValue >
499+ > ( )
497500 } )
498501
499502 test ( 'createAsyncThunk without generics, accessing `api` does not break return type' , ( ) => {
500503 const thunk = createAsyncThunk ( 'test' , ( _ : void , api ) => {
501504 return 'ret' as const
502505 } )
503506
504- expectTypeOf ( thunk ) . toEqualTypeOf < AsyncThunk < 'ret' , void , { } > > ( )
507+ expectTypeOf ( thunk ) . toEqualTypeOf <
508+ AsyncThunk < 'ret' , void , AnyNonNullishValue >
509+ > ( )
505510 } )
506511
507512 test ( 'createAsyncThunk rejectWithValue without generics: Expect correct return type' , ( ) => {
@@ -551,17 +556,26 @@ describe('type tests', () => {
551556 }
552557
553558 // has to stay on one line or type tests fail in older TS versions
554- // prettier-ignore
555- // @ts -expect-error
556- const shouldFail = createAsyncThunk ( 'without generics' , ( ) => { } , { serializeError : funkySerializeError } )
559+ const shouldFail = createAsyncThunk (
560+ 'without generics' ,
561+ ( ) => {
562+ /* No-Op */
563+ } ,
564+ // @ts -expect-error
565+ { serializeError : funkySerializeError } ,
566+ )
557567
558568 const shouldWork = createAsyncThunk <
559569 any ,
560570 void ,
561571 { serializedErrorType : Funky }
562- > ( 'with generics' , ( ) => { } , {
563- serializeError : funkySerializeError ,
564- } )
572+ > (
573+ 'with generics' ,
574+ ( ) => {
575+ /* No-Op */
576+ } ,
577+ { serializeError : funkySerializeError } ,
578+ )
565579
566580 if ( shouldWork . rejected . match ( unknownAction ) ) {
567581 expectTypeOf ( unknownAction . error ) . toEqualTypeOf < Funky > ( )
@@ -571,33 +585,54 @@ describe('type tests', () => {
571585 test ( '`idGenerator` option takes no arguments, and returns a string' , ( ) => {
572586 const returnsNumWithArgs = ( foo : any ) => 100
573587 // has to stay on one line or type tests fail in older TS versions
574- // prettier-ignore
575- // @ts -expect-error
576- const shouldFailNumWithArgs = createAsyncThunk ( 'foo' , ( ) => { } , { idGenerator : returnsNumWithArgs } )
588+ const shouldFailNumWithArgs = createAsyncThunk (
589+ 'foo' ,
590+ ( ) => {
591+ /* No-Op */
592+ } ,
593+ // @ts -expect-error
594+ { idGenerator : returnsNumWithArgs } ,
595+ )
577596
578597 const returnsNumWithoutArgs = ( ) => 100
579- // prettier-ignore
580- // @ts -expect-error
581- const shouldFailNumWithoutArgs = createAsyncThunk ( 'foo' , ( ) => { } , { idGenerator : returnsNumWithoutArgs } )
598+ const shouldFailNumWithoutArgs = createAsyncThunk (
599+ 'foo' ,
600+ ( ) => {
601+ /* No-Op */
602+ } ,
603+ // @ts -expect-error
604+ { idGenerator : returnsNumWithoutArgs } ,
605+ )
582606
583607 const returnsStrWithNumberArg = ( foo : number ) => 'foo'
584- // prettier-ignore
585- // @ts -expect-error
586- const shouldFailWrongArgs = createAsyncThunk ( 'foo' , ( arg : string ) => { } , { idGenerator : returnsStrWithNumberArg } )
608+ const shouldFailWrongArgs = createAsyncThunk (
609+ 'foo' ,
610+ ( arg : string ) => {
611+ /* No-Op */
612+ } ,
613+ // @ts -expect-error
614+ { idGenerator : returnsStrWithNumberArg } ,
615+ )
587616
588617 const returnsStrWithStringArg = ( foo : string ) => 'foo'
589618 const shoulducceedCorrectArgs = createAsyncThunk (
590619 'foo' ,
591- ( arg : string ) => { } ,
620+ ( arg : string ) => {
621+ /* No-Op */
622+ } ,
592623 {
593624 idGenerator : returnsStrWithStringArg ,
594625 } ,
595626 )
596627
597628 const returnsStrWithoutArgs = ( ) => 'foo'
598- const shouldSucceed = createAsyncThunk ( 'foo' , ( ) => { } , {
599- idGenerator : returnsStrWithoutArgs ,
600- } )
629+ const shouldSucceed = createAsyncThunk (
630+ 'foo' ,
631+ ( ) => {
632+ /* No-Op */
633+ } ,
634+ { idGenerator : returnsStrWithoutArgs } ,
635+ )
601636 } )
602637
603638 test ( 'fulfillWithValue should infer return value' , ( ) => {
@@ -633,8 +668,14 @@ describe('type tests', () => {
633668
634669 test ( 'meta return values' , ( ) => {
635670 // return values
636- createAsyncThunk < 'ret' , void , { } > ( 'test' , ( _ , api ) => 'ret' as const )
637- createAsyncThunk < 'ret' , void , { } > ( 'test' , async ( _ , api ) => 'ret' as const )
671+ createAsyncThunk < 'ret' , void , AnyNonNullishValue > (
672+ 'test' ,
673+ ( _ , api ) => 'ret' as const ,
674+ )
675+ createAsyncThunk < 'ret' , void , AnyNonNullishValue > (
676+ 'test' ,
677+ async ( _ , api ) => 'ret' as const ,
678+ )
638679 createAsyncThunk < 'ret' , void , { fulfilledMeta : string } > ( 'test' , ( _ , api ) =>
639680 api . fulfillWithValue ( 'ret' as const , '' ) ,
640681 )
0 commit comments