Skip to content

Commit

Permalink
TS Improvements (#1676)
Browse files Browse the repository at this point in the history
* Do not truncate errors, pretty output

* Introduce helpers for sagas

* Update yarn lock
  • Loading branch information
HenryNguyen5 authored and dternyak committed May 21, 2018
1 parent d5013b8 commit b40c2a9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
22 changes: 22 additions & 0 deletions shared/types/sagaHelpers.d.ts
@@ -0,0 +1,22 @@
import { Effect } from 'redux-saga/effects';

type ExtPromise<T> = T extends Promise<infer U> ? U : T;

type ExtSaga<T> = T extends IterableIterator<infer U> ? Exclude<U, Effect | Effect[]> : T;

/**
* Use this helper to unwrap return types from effects like Call / Apply
* In the case of calling a function that returns a promise, this helper will unwrap the
* promise and return the type inside it. In the case of calling another saga / generator,
* this helper will return the actual return value of the saga / generator, otherwise,
* it'll return the original type.
*
* NOTE 1: When using this to extract the type of a Saga, make sure to remove the `SagaIterator`
* return type of the saga if it contains one, since that masks the actual return type of the saga.
*
* NOTE 2: You will most likely need to use the `typeof` operator to use this helper.
* E.g type X = UnwrapEffects<typeof MyFunc/MySaga>
*/
export type UnwrapEffects<T> = T extends (...args: any[]) => any
? ExtSaga<ReturnType<T>>
: ExtPromise<T>;
2 changes: 2 additions & 0 deletions tsconfig.json
Expand Up @@ -18,6 +18,8 @@
"noEmitOnError": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true,
"noErrorTruncation": true,
"noImplicitAny": true
},
"include": [
Expand Down

0 comments on commit b40c2a9

Please sign in to comment.