-
-
Notifications
You must be signed in to change notification settings - Fork 587
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
Action should not have handler required in interface definition #467
Comments
Good catch. Please create a fix PR. Thanks in advance! |
The same issue with event handlers, I think... |
You're probably right. It's not as obvious a use case since a |
Hey guys! I'm heavily modifying the typings for Moleculer in order to provide perfect autocomplete and type-safety for both actions definitions and Since this requires several in-depth changes, I copy/pasted the I just stumbled on a small problem caused (in part) by this issue: The type The issue is that the definition of {
name?: string;
visibility?: ActionVisibility;
params?: ActionParams;
service?: Service;
cache?: boolean | ActionCacheOptions;
handler?: ActionHandler;
tracing?: boolean | TracingActionOptions;
bulkhead?: BulkheadOptions;
circuitBreaker?: BrokerCircuitBreakerOptions;
retryPolicy?: RetryPolicyOptions;
fallback?: string | FallbackHandler;
hooks?: ActionHooks;
[key: string]: any;
} One side-effect of this situation is that any function will match this type because any function has string properties with Meaning it throws off my error detection because using a function that returns a Since it's a pretty big deal for the perfect type-safety I'm working on, I'd like to find a workaround which would be satisfying for everyone. The best solution I could think of was to be harsher on the type of the index signature and not use After tinkering, I thought I could use a pretty generous union: string | boolean | any[] | number | Record<any, any> | null | undefined However, I'm not a moleculer god and aren't really aware of what strange patterns could be used in the shadows. @shawnmcknight @icebob Do you have any feedback regarding this specific situation? Thanks in advance for reading! |
You can't limit the action schema because it allows defining any other custom property. E.g. you can write a middleware which checks a custom property in all actions. If you don't allow it, many users can't use custom middleware/mixins. The boolean action definition is a method to disable an action which comes from a mixin. |
I totally understand your point, yes. I don't want it to be too constrained or it will be less convenient and that's why I wanted to use a very permissive union instead. |
For future reference, I asked a question about my comment on StackOverflow: https://stackoverflow.com/questions/64305700/prevent-very-permissive-object-type-from-matching-any-function |
Prerequisites
Please answer the following questions for yourself before submitting an issue.
Expected Behavior
Allow action to be defined without a
handler
property in TypeScript.Current Behavior
Interface definition for
Action
is:This marks the
handler
as required.Failure Information
Technically speaking, every action ultimately needs a handler. However, in my use case I have created a Mixin which defines the
handler
. The service is adding additional properties to augment the mixin (e.g.cache
). In this manner thehandler
defined in the Mixin is reusable and allows the service to define properties dynamically.Steps to Reproduce
handler
.handler
(e.g.cache
).At runtime, this configuration will work because the action definition is merged deeply into the mixin. However, TypeScript will not allow it to be compiled.
Reproduce code snippet
Mixin:
Service:
Context
My thought process here is to simply mark the handler as an optional property of the
Action
interface. I'm happy to create a PR if there is agreement on that.Thanks!
The text was updated successfully, but these errors were encountered: