redux-logic icon indicating copy to clipboard operation
redux-logic copied to clipboard

Why is CreateLogic.Config.Transform.Hook uses optional argument for reject?

Open chenxinyanc opened this issue 5 years ago • 2 comments

When compiling with tsc and --strict switched on, I found error in createLogic callback. Specifically, tsc indicates reject parameter in transform function can be undefined. I checked the type definition: https://github.com/jeffbski/redux-logic/blob/4b0951af03dbe3097e4bad5710fe274c2f3f1e6b/definitions/logic.d.ts#L216-L240

It used reject?: Pass<Action, Context> for transform function signature. I wonder, is there any case where transform can be called with reject neglected? I've checked #124 and #125, but still couldn't figure out why there is ? following reject parameter.

chenxinyanc avatar Jul 29 '19 08:07 chenxinyanc

The reason that reject is optional in the signature is that transform and validate are just aliases for the same method. validate would necessarily need allow/next and reject. The reason for both is simply to better communicate the intent. Reject wouldn't usually be needed in a transform use case, but left as optional to mirror the validate signature.

jeffbski avatar Aug 14 '19 22:08 jeffbski

The reason that reject is optional in the signature is that transform and validate are just aliases for the same method.

If I've got this correct, transform and validate are actually the same callback.

https://github.com/jeffbski/redux-logic/blob/4b0951af03dbe3097e4bad5710fe274c2f3f1e6b/src/createLogic.js#L175-L177

In this case, I think it would be better if you either

  • make reject in transform a non-optional parameter, so when I'm implementing the callback, I do not need to write something like
if (reject) {
    reject(action);
}

because there is actually no case where reject is null, but I will need to make tsc happy. However, you may write some @deprecate notice in tsdoc or some runtime warning to tell me do not use transform for rejct.

  • just remove reject parameter in .d.ts for transform, so to force me to choose validate callback because it has a non-optional reject parameter

https://github.com/jeffbski/redux-logic/blob/4b0951af03dbe3097e4bad5710fe274c2f3f1e6b/definitions/logic.d.ts#L201-L212

chenxinyanc avatar Aug 16 '19 06:08 chenxinyanc