简体   繁体   中英

Typescript interface to define a generator

I have some code that look like this:

const saga =  function* (action) {
        yield put({
            type: actions.SUCCESS,
            payload: action.payload
        });
    };


const sagaWatcher =  createDefaultSagaWatcher(actions, saga); 

That I am converting to TypeScript.

I need to create an interface for the generator function, so I can add type checking on the createDefaultSagaWatcher function. How do I do this?

I've tried doing something like this:

interface ReduxSaga {
    (action: ReduxAction)* : any; 
}

but that syntax is incorrect.

Much like the async modifier, the * is an implementation detail. The important part for the caller of the function is that the function returns something that can be iterated, how that iteration is implemented should not matter to the caller. Since you want the iteration to return anything you can use IterableIterator<any>

interface ReduxSaga {
    (action: ReduxAction): IterableIterator<any>; 
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM