-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: Expose FunctionWithParametersType #2983
Comments
Before we add this, check to see if this response helps solve your issue. #2532 (comment) |
I spent over an hour trying all of the publicly exposed types to get our types to work with NgRx. In the end, the only one that allowed all the typing to align was |
To answer your question @brandonroberts , Alex's suggestions do not work for our use case actually. Would it be a big deal to expose that Type? |
Maybe. Everything we export publicly makes it harder to change in the future if we need to because of external dependencies like this use case. That's why I asked if it could be achieved without doing so. |
I'm pretty sure what we are trying to achieve cannot be done any other way. I've run through the gamut of types, and this was the only one that gave us proper compatibility with NgRx. I've actually run into this need a few times in the past, and always ended up deep importing. Basically, any time you want to do something that reflects the nature of the dispatch function, you need this type. It's a pretty foundational type to how actions and dispatching works. |
@jrista So we can understand further, can you explain what you're actually trying to do? All actions already can be dispatched without having a dispatch in their signaure directly, so why is something custom needed here? |
We are working on a simple library that makes it easier to use NgRx within Angular component templates. We have a simple utility function that creates a "dispatcher" function for a given action. We attach those dispatchers to the component class, so we can call them directly from within the template. Its a super tiny, light weight library, but it can save a fair amount of coding in applications. This little utility function, in order to get it working with NgRx properly, requires the The one type that works is |
export const smartDispatch = <
T extends string = string,
C extends Creator = Creator,
P extends any[] = any[],
// eslint-disable-next-line @typescript-eslint/ban-types
R extends object = object
>(
action: ActionCreator<T, C>
): FunctionWithParametersType<P, R> => {
const dispatch = (...args: P): R => {
const store = (InjectorLocator as any)._injector.get(Store);
return store.dispatch(action(args));
};
return dispatch;
}; This is what I fiddled with for about an hour or so, before I finally settled on the export const refreshItemsButtonClicked = createAction('[MyComponent] (Refresh Items Button) Clicked');
@Component(...)
export class MyComponent {
refresh = smartDispatch(refreshItemsButtonClicked);
} |
Describe any alternatives/workarounds you're currently using
Is there a way to expose
FunctionWithParametersType
? I have several needs to reference this Type but I am only able to access it by doing a deep import@ngrx/store/models/src/
.I tried the following scenario using Creator but I get the following error:
Type 'R' does not satisfy the constraint 'object'.
If so, is it possible to patch this in previous versions as well?
Other information:
If accepted, I would be willing to submit a PR for this feature
[ X] Yes (Assistance is provided if you need help submitting a pull request)
[ ] No
The text was updated successfully, but these errors were encountered: