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

computedFrom type guard when using filter #76

Closed
tomer953 opened this issue Sep 21, 2023 · 2 comments
Closed

computedFrom type guard when using filter #76

tomer953 opened this issue Sep 21, 2023 · 2 comments

Comments

@tomer953
Copy link
Contributor

Expected:
computedFrom operator should allow type narrowing
Small example using filterNil() to reproduce:

n = signal<number | undefined | null>(3);

  // filter with typeguard is working
  x$ = of(this.n()).pipe(
    filter((x): x is number => Boolean(x)),
    map((x) => (x as number) + 1)
  );
 
  // filterNil has typeguard already
  y$ = of(this.n()).pipe(
    filterNil(),
    map((x) => x + 1)
  );

  // but with computedFrom it failes to narrow
  z$ = computedFrom(
    [this.n],
    pipe(
      filterNil(),
      map(([x]) => x + 1) // x is possibly null or undefined
    )
  );

z$ should act like y$
I can’t find why this happens, @nartc might help here

Originally posted by @tomer953 in #73 (comment)

@tomalaforge
Copy link
Collaborator

`filterNil' only works for simple values, not array, and computedFrom is passing an array of element to your operator function

We would need a filterArrayNil or something similar

@tomer953
Copy link
Contributor Author

oh i see. i thought the computedFrom flatten the array behind the scenes.
so actually its not broken. my bad

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

No branches or pull requests

2 participants