-
Notifications
You must be signed in to change notification settings - Fork 90
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
Swap computedFrom<Input, Output> to computedFrom<Output, Input>? #73
Comments
Hey, the first feedback makes sense to me but I don't quite get the 2nd feedback. Can you elaborate a bit? |
I think he wants to spread the args |
@tomer953 that's gonna be painful to type 😅 but totally understand the desire. |
besides that, |
Hey Chau, yea I can elaborate. The beauty of working with Use my last example: function filterNotNullOrUndefined<T>(
input: null | undefined | T
): input is T {
return input !== null && input !== undefined;
}
// I use this function alot in my codebase
function rxjsFilterNotNullOrUndefined<T>() {
return (source$: Observable<null | undefined | T>) =>
source$.pipe(filter(filterNotNullOrUndefined));
}
// Desirable
computedNumberToSomething = computedFrom<number>(
this.someNumber, // imagine this is <number | null | undefined>
pipe(
// now I want to filter the typeguard function above, this is now NonNullable<number>
rxjsFilterNotNullOrUndefined(),
map((number) => someNumber + 1)
)
);
// Current
computedNumberToSomething = computedFrom<number>(
[this.someNumber], // imagine this is <number | null | undefined>
pipe(
// typeguard won't infer the 1st param is NonNullable<number>
filter(([number]) => !!number),
// Error: number can be undefined or null
map(([number]) => someNumber + 1 )
)
);
Unless you have a better way to handle these situations, I'm all ears :) Renato |
The problem you mentioned is not related to this library but its an old and famous problem in typescript |
B.t.w:
function filterNotNullOrUndefined<T>(
input: null | undefined | T
): input is T {
return input !== null && input !== undefined;
}
// I use this function alot in my codebase
function rxjsFilterNotNullOrUndefined<T>() {
return (source$: Observable<null | undefined | T>) =>
source$.pipe(filter(filterNotNullOrUndefined));
} Check out the brand new |
Yea, I know this issue, but we can have some workarounds to mirigate this limitation, my suggestion was targeted for this. |
I'll take a closer look this weekend. |
I now have time to take a look at this.
// Desirable
computedNumberToSomething = computedFrom<number>(
this.someNumber, // imagine this is <number | null | undefined>
pipe(
// now I want to filter the typeguard function above, this is now NonNullable<number>
rxjsFilterNotNullOrUndefined(),
map((number) => someNumber + 1)
)
); Your desirable case won't work because what happens if: // Desirable
computedNumberToSomething = computedFrom<number>(
this.someNumber, // imagine this is <number | null | undefined>
this.someOtherValue,
pipe(
// now I want to filter the typeguard function above, this is now NonNullable<number>
rxjsFilterNotNullOrUndefined(), // now what happens?
map((number) => someNumber + 1)
)
); Regardless of how we want computedFrom(
[of<number | null>(1), signal(2)],
pipe(
filter(([first]) => !!first), // here we're saying if first is falsy, then stream won't emit.
map(([first, second]) => {})
)
);
// in this case, I'd recommend
computedFrom(
[of<number | null>(1).pipe(filterNil()), signal(2)],
pipe(
map(([first, second]) => {})
)
) |
@renatoaraujoc looks like this is not doable. Is it ok if we close this? |
Hey @eneajaho, it should be doable on next versions of TS but today it's definitely not. Let's close it :) |
Hello @nartc and @eneajaho,
After a talk with Enea on Twitter, I've shown that maybe we could improve computedFrom type inference.
Right now, we can't easily tell
computedFrom
what kind of Output type we want it to be unless we specify the Input params first.Right now we need to do this:
Could we have something like:
It would be needed to swap <Input, Output> to <Output, Input> in
computedFrom
.Second feedback:
Create an overloaded function that also accepts a non readonly array, so we can:
What yall think?
Renato
The text was updated successfully, but these errors were encountered: