@@ -3,7 +3,7 @@ import { ThunkDispatch } from 'redux-thunk'
3
3
import { unwrapResult , SerializedError } from 'src/createAsyncThunk'
4
4
5
5
import apiRequest , { AxiosError } from 'axios'
6
- import { IsAny } from 'src/tsHelpers'
6
+ import { IsAny , IsUnknown } from 'src/tsHelpers'
7
7
8
8
function expectType < T > ( t : T ) {
9
9
return t
@@ -181,3 +181,164 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
181
181
}
182
182
} )
183
183
}
184
+
185
+ /**
186
+ * payloadCreator first argument type has impact on asyncThunk argument
187
+ */
188
+ {
189
+ // no argument: asyncThunk has no argument
190
+ {
191
+ const asyncThunk = createAsyncThunk ( 'test' , ( ) => 0 )
192
+ expectType < ( ) => any > ( asyncThunk )
193
+ // typings:expect-error cannot be called with an argument
194
+ asyncThunk ( 0 as any )
195
+ }
196
+
197
+ // one argument, specified as undefined: asyncThunk has no argument
198
+ {
199
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : undefined ) => 0 )
200
+ expectType < ( ) => any > ( asyncThunk )
201
+ // typings:expect-error cannot be called with an argument
202
+ asyncThunk ( 0 as any )
203
+ }
204
+
205
+ // one argument, specified as void: asyncThunk has no argument
206
+ {
207
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : void ) => 0 )
208
+ expectType < ( ) => any > ( asyncThunk )
209
+ // typings:expect-error cannot be called with an argument
210
+ asyncThunk ( 0 as any )
211
+ }
212
+
213
+ // one argument, specified as optional number: asyncThunk has optional number argument
214
+ // this test will fail with strictNullChecks: false, that is to be expected
215
+ // in that case, we have to forbid this behaviour or it will make arguments optional everywhere
216
+ {
217
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg ?: number ) => 0 )
218
+ expectType < ( arg ?: number ) => any > ( asyncThunk )
219
+ asyncThunk ( )
220
+ asyncThunk ( 5 )
221
+ // typings:expect-error
222
+ asyncThunk ( 'string' )
223
+ }
224
+
225
+ // one argument, specified as number|undefined: asyncThunk has optional number argument
226
+ // this test will fail with strictNullChecks: false, that is to be expected
227
+ // in that case, we have to forbid this behaviour or it will make arguments optional everywhere
228
+ {
229
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : number | undefined ) => 0 )
230
+ expectType < ( arg ?: number ) => any > ( asyncThunk )
231
+ asyncThunk ( )
232
+ asyncThunk ( 5 )
233
+ // typings:expect-error
234
+ asyncThunk ( 'string' )
235
+ }
236
+
237
+ // one argument, specified as number|void: asyncThunk has optional number argument
238
+ {
239
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : number | void ) => 0 )
240
+ expectType < ( arg ?: number ) => any > ( asyncThunk )
241
+ asyncThunk ( )
242
+ asyncThunk ( 5 )
243
+ // typings:expect-error
244
+ asyncThunk ( 'string' )
245
+ }
246
+
247
+ // one argument, specified as any: asyncThunk has required any argument
248
+ {
249
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : any ) => 0 )
250
+ expectType < IsAny < Parameters < typeof asyncThunk > [ 0 ] , true , false > > ( true )
251
+ asyncThunk ( 5 )
252
+ // typings:expect-error
253
+ asyncThunk ( )
254
+ }
255
+
256
+ // one argument, specified as unknown: asyncThunk has required unknown argument
257
+ {
258
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : unknown ) => 0 )
259
+ expectType < IsUnknown < Parameters < typeof asyncThunk > [ 0 ] , true , false > > ( true )
260
+ asyncThunk ( 5 )
261
+ // typings:expect-error
262
+ asyncThunk ( )
263
+ }
264
+
265
+ // one argument, specified as number: asyncThunk has required number argument
266
+ {
267
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : number ) => 0 )
268
+ expectType < ( arg : number ) => any > ( asyncThunk )
269
+ asyncThunk ( 5 )
270
+ // typings:expect-error
271
+ asyncThunk ( )
272
+ }
273
+
274
+ // two arguments, first specified as undefined: asyncThunk has no argument
275
+ {
276
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : undefined , thunkApi ) => 0 )
277
+ expectType < ( ) => any > ( asyncThunk )
278
+ // typings:expect-error cannot be called with an argument
279
+ asyncThunk ( 0 as any )
280
+ }
281
+
282
+ // two arguments, first specified as void: asyncThunk has no argument
283
+ {
284
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : void , thunkApi ) => 0 )
285
+ expectType < ( ) => any > ( asyncThunk )
286
+ // typings:expect-error cannot be called with an argument
287
+ asyncThunk ( 0 as any )
288
+ }
289
+
290
+ // two arguments, first specified as number|undefined: asyncThunk has optional number argument
291
+ // this test will fail with strictNullChecks: false, that is to be expected
292
+ // in that case, we have to forbid this behaviour or it will make arguments optional everywhere
293
+ {
294
+ const asyncThunk = createAsyncThunk (
295
+ 'test' ,
296
+ ( arg : number | undefined , thunkApi ) => 0
297
+ )
298
+ expectType < ( arg ?: number ) => any > ( asyncThunk )
299
+ asyncThunk ( )
300
+ asyncThunk ( 5 )
301
+ // typings:expect-error
302
+ asyncThunk ( 'string' )
303
+ }
304
+
305
+ // two arguments, first specified as number|void: asyncThunk has optional number argument
306
+ {
307
+ const asyncThunk = createAsyncThunk (
308
+ 'test' ,
309
+ ( arg : number | void , thunkApi ) => 0
310
+ )
311
+ expectType < ( arg ?: number ) => any > ( asyncThunk )
312
+ asyncThunk ( )
313
+ asyncThunk ( 5 )
314
+ // typings:expect-error
315
+ asyncThunk ( 'string' )
316
+ }
317
+
318
+ // two arguments, first specified as any: asyncThunk has required any argument
319
+ {
320
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : any , thunkApi ) => 0 )
321
+ expectType < IsAny < Parameters < typeof asyncThunk > [ 0 ] , true , false > > ( true )
322
+ asyncThunk ( 5 )
323
+ // typings:expect-error
324
+ asyncThunk ( )
325
+ }
326
+
327
+ // two arguments, first specified as unknown: asyncThunk has required unknown argument
328
+ {
329
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : unknown , thunkApi ) => 0 )
330
+ expectType < IsUnknown < Parameters < typeof asyncThunk > [ 0 ] , true , false > > ( true )
331
+ asyncThunk ( 5 )
332
+ // typings:expect-error
333
+ asyncThunk ( )
334
+ }
335
+
336
+ // two arguments, first specified as number: asyncThunk has required number argument
337
+ {
338
+ const asyncThunk = createAsyncThunk ( 'test' , ( arg : number , thunkApi ) => 0 )
339
+ expectType < ( arg : number ) => any > ( asyncThunk )
340
+ asyncThunk ( 5 )
341
+ // typings:expect-error
342
+ asyncThunk ( )
343
+ }
344
+ }
0 commit comments