From b40c2a95084da694512c46a745c20c7d1e3e2c92 Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Mon, 21 May 2018 14:39:03 -0400 Subject: [PATCH] TS Improvements (#1676) * Do not truncate errors, pretty output * Introduce helpers for sagas * Update yarn lock --- shared/types/sagaHelpers.d.ts | 22 ++++++++++++++++++++++ tsconfig.json | 2 ++ 2 files changed, 24 insertions(+) create mode 100644 shared/types/sagaHelpers.d.ts diff --git a/shared/types/sagaHelpers.d.ts b/shared/types/sagaHelpers.d.ts new file mode 100644 index 00000000..812a869d --- /dev/null +++ b/shared/types/sagaHelpers.d.ts @@ -0,0 +1,22 @@ +import { Effect } from 'redux-saga/effects'; + +type ExtPromise = T extends Promise ? U : T; + +type ExtSaga = T extends IterableIterator ? Exclude : 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 + */ +export type UnwrapEffects = T extends (...args: any[]) => any + ? ExtSaga> + : ExtPromise; diff --git a/tsconfig.json b/tsconfig.json index f878f0a4..11fd0130 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -18,6 +18,8 @@ "noEmitOnError": false, "noUnusedLocals": true, "noUnusedParameters": true, + "pretty": true, + "noErrorTruncation": true, "noImplicitAny": true }, "include": [