File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -8,6 +8,8 @@ type NotifyFunction = (callback: () => void) => void
88
99type BatchNotifyFunction = ( callback : ( ) => void ) => void
1010
11+ type BatchCallsCallback < T extends unknown [ ] > = ( ...args : T ) => void
12+
1113export function createNotifyManager ( ) {
1214 let queue : NotifyCallback [ ] = [ ]
1315 let transactions = 0
@@ -45,12 +47,14 @@ export function createNotifyManager() {
4547 /**
4648 * All calls to the wrapped function will be batched.
4749 */
48- const batchCalls = < T extends Function > ( callback : T ) : T => {
49- return ( ( ...args : any [ ] ) => {
50+ const batchCalls = < T extends unknown [ ] > (
51+ callback : BatchCallsCallback < T > ,
52+ ) : BatchCallsCallback < T > => {
53+ return ( ...args ) => {
5054 schedule ( ( ) => {
5155 callback ( ...args )
5256 } )
53- } ) as any
57+ }
5458 }
5559
5660 const flush = ( ) : void => {
Original file line number Diff line number Diff line change @@ -48,4 +48,19 @@ describe('notifyManager', () => {
4848
4949 expect ( notifySpy ) . toHaveBeenCalledTimes ( 1 )
5050 } )
51+
52+ it ( 'typedefs should catch proper signatures' , async ( ) => {
53+ const notifyManagerTest = createNotifyManager ( )
54+
55+ // we define some fn with its signature:
56+ const fn : ( a : string , b : number ) => string = ( a , b ) => a + b
57+
58+ //now somefn expect to be called with args [a: string, b: number]
59+ const someFn = notifyManagerTest . batchCalls ( fn )
60+
61+ someFn ( 'im happy' , 4 )
62+
63+ //@ts -expect-error
64+ someFn ( 'im not happy' , false )
65+ } )
5166} )
You can’t perform that action at this time.
0 commit comments