Skip to content

Better typings for dataPersistence #4108

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

Closed
rafaelss95 opened this issue Nov 18, 2020 · 3 comments
Closed

Better typings for dataPersistence #4108

rafaelss95 opened this issue Nov 18, 2020 · 3 comments
Labels
community This is a good first issue for contributing outdated scope: angular Issues related to Angular support in Nx stale type: enhancement

Comments

@rafaelss95
Copy link

rafaelss95 commented Nov 18, 2020

Problems

  1. state on dataPersistence callbacks is nullable.

export interface FetchOpts<T, A> {
id?(a: A, state?: T): any;
run(a: A, state?: T): Observable<Action> | Action | void;
onError?(a: A, e: any): Observable<any> | any;
}

export function fetch<T, A extends Action>(opts: FetchOpts<T, A>) {
return (source: ActionStateStream<T, A>): Observable<Action> => {

With strict type enabled, we can't use the state directly without checking its nullability before. Ex:

readonly test$ = createEffect(() =>
  this.actions$.pipe(
    concatMap(action => of(action).pipe(
      withLatestFrom(this.store$.select(someSelector)),
    ),
    navigation(AComponent, {
      run: (_, state) => {
        state.awesome // Type error as state is nullable
      },
    }),
  ),
);
  1. Conditional return:
  • If it's a non-dispatching effect then it should be void;
  • If it's a dispatching effect then it could be either Action or Observable<Action>.

Note that currently I can write something like this:

readonly test$ = createEffect(() =>
  this.actions$.pipe(
    navigation(AComponent, {
      run: () => {
        someAction(); // missing return intentionally
      },
    }),
  ),
);

... without type errors :(. It would be awesome if we conditionally defined the correct type to avoid mistakes like this.

  1. In documentation you say:

* `fetch` addresses these problems--it runs all fetches in order, which removes race conditions
* and forces the developer to handle errors.

"[...] and forces the developer to handle errors [...]"

... however the onError is optional. Maybe I just misunderstood the sentence?

  1. Change from any to unknown in onError, in order to avoid some typos, while accessing props. Somewhat related to Error in catch clause should be unknown microsoft/TypeScript#26174.

Suggestions

For the points 1, 3 and 4, I got this idea (not sure if it's the best way, but...):

export interface FetchOpts<T, A> { 
-  id?(a: A, state?: T): any; 
+  id?(a: A, state: T): unknown;
-  run(a: A, state?: T): Observable<Action> | Action | void; 
+  run(a: A, state: T): Observable<Action> | Action | void;
-  onError?(a: A, e: any): Observable<any> | any; 
+  onError(a: A, e: unknown): unknown;
 } 

Environment

Angular 10
Ngrx 10
NX 10

@brandonroberts
Copy link
Contributor

@rafaelss95 better typings would be an improvement. Would you like to put in a PR for this?

@brandonroberts brandonroberts added the community This is a good first issue for contributing label Feb 5, 2021
@github-actions
Copy link

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 🙏

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
community This is a good first issue for contributing outdated scope: angular Issues related to Angular support in Nx stale type: enhancement
Projects
None yet
Development

No branches or pull requests

3 participants