Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
chenxinyanc opened this issue Jul 29, 2019 · 2 comments

Comments

@chenxinyanc
Copy link

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:

/* ----- transform ----- */
interface Transform<
State,
Action extends ActionBasis,
Dependency extends object,
Context extends Object
> {
transform?: Transform.Hook<State, Action, Dependency, Context>;
}
export namespace Transform {
export type Hook<
State,
Action extends ActionBasis,
Dependency extends object,
Context extends Object = undefined
> = (
depObj: DepObj<State, Action, Dependency>,
next: Pass<Action, Context>,
reject?: Pass<Action, Context>
) => void;
}
// ---------------------------------------- //

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.

@jeffbski
Copy link
Owner

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.

@chenxinyanc
Copy link
Author

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.

if (validate && transform) {
throw new Error('logic cannot define both the validate and transform hooks they are aliases');
}

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

export namespace Validate {
export type Hook<
State,
Action extends ActionBasis,
Dependency extends object,
Context extends Object = undefined
> = (
depObj: DepObj<State, Action, Dependency>,
allow: Pass<Action, Context>,
reject: Pass<Action, Context>
) => void;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants