Closed tvedtorama closed 3 years ago
Not sure if it helps, but here is the definition of filter from ixjs, which supports type guards:
export declare function filter<T, S extends T>(predicate: (value: T, index: number) => value is S, thisArg?: any): OperatorFunction<T, S>;
@ulfryk sorry for slight delay. I think we can update filter
typing to help with that.
Also you can try creating a type checking funcion as a workaround for now:
const isAType = (v: {x: "A", value: number} | {x: "B", noValueHere: string}): v is {x: "A", value: number} => v.x === "A";
and then below should work (didn't check for 100%):
const result = Some(value)
.filter(isAType)
.map(item => item.value)
.orSome(-1)
@ulfryk You would need the function anyways, my original sample - with just a check, no function - would probably not work in any circumstances. But I don't think the current implementation will cut it. I modified your example slightly:
type T = {x: "A", value: number} | {x: "B", noValueHere: string}
const isAType = (v: T): v is {x: "A", value: number} => v.x === "A";
const value: T = null // Or anything, but if concrete - TS fools us into believing this works
const result = Some(value)
.filter(isAType)
.map(item => item.value) // Error
.orSome(-1)
The reason being that filter returns a Monad of the same type, no option to modify it:
filter(fn: (val: T) => boolean): Maybe<T>;
@tvedtorama you are completely right. Do you think you can create PR with a fix for filter
typings ?
Maybe - I'll put it on my list. This is a problem I run into now and then - so it would make sense.
I took some time to work on this and opened #240, let me know if something like this could work for your use case @tvedtorama
@tvedtorama can you check if nightly 0.9.1-466
works as you need ?
I'm not sure if I got the skills or time to check this right now, but I looked at the code/tests - and it looks perfect!
OK works --> https://stackblitz.com/edit/typescript-xyxmhl
I will clean things up and release 0.9.2
this week
A new version of the package monet (0.9.2) was published at 2021-03-05T17:36:17.909Z from
35.227.97.188. The shasum of this package was c59d4390937a1b021126844b50b993963c6b0ec7.
I have a quite frequent problem, where I make an assertion in
filter
which determines type. For the followingmap
I expect that the type to be adapted based on the new information. It is not, the output typeT
is the same as in the input infilter
.Example:
This errors on the
map
following thefilter
.A workaround:
I don't know if this is easy/possible to fix, but it would be very nice.