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

Support user defined type guards in filter predicates #44

Closed
OliverJAsh opened this issue Sep 20, 2017 · 10 comments · Fixed by #56 or #83
Closed

Support user defined type guards in filter predicates #44

OliverJAsh opened this issue Sep 20, 2017 · 10 comments · Fixed by #56 or #83

Comments

@OliverJAsh
Copy link

OliverJAsh commented Sep 20, 2017

IxJS version:
2.1.0

Code to reproduce:

// no error, good!
// TypeScript's Array typings support user defined type guards in the filter predicate
const xs: number[] = ['foo', 1].filter((x): x is number => x instanceof Number);

// unexpected error
const xs2: Ix.Iterable<number> = Ix.Iterable
    .from(['foo', 1])
    .filter((x): x is number => typeof x === 'number');
@ichpuchtli
Copy link

wow I didn't know that that's awesome.

@trxcllnt
Copy link
Member

trxcllnt commented Oct 5, 2017

@OliverJAsh 🎉 thanks!

@OliverJAsh
Copy link
Author

@trxcllnt Using 2.1.4, the example above still seems to generate an error. Any ideas?

@trxcllnt
Copy link
Member

trxcllnt commented Oct 9, 2017

@OliverJAsh I don't think they made it in the 2.1.4 release, will circle back with @mattpodwysocki and do another release soon

@trxcllnt
Copy link
Member

trxcllnt commented Oct 15, 2017

@OliverJAsh just pushed this out in 2.2.0, but also be aware of this breaking change to zip when you update

@OliverJAsh
Copy link
Author

I'm using 2.2.0 and the example above still seems to generate an error. Any ideas? @trxcllnt

❯ grep version node_modules/ix/package.json
  "version": "2.2.0",

image

@trxcllnt
Copy link
Member

trxcllnt commented Oct 15, 2017

@OliverJAsh oh you're right, looks like I forgot to extend the generic type:

edit hm this is weird. trying to figure it out, gimme a bit.

There's still the issue that 1 instanceof Number evaluates to false, so the runtime behavior won't be what you expect even though TS gives the OK. I ran into that in the type guards test

trxcllnt added a commit that referenced this issue Oct 17, 2017
also ran prettier on everything, so there's that :/

closes #44 for real now tho
trxcllnt added a commit that referenced this issue Oct 17, 2017
also ran prettier on everything, so there's that :/

closes #44 for real now tho
mattpodwysocki pushed a commit that referenced this issue Oct 17, 2017
also ran prettier on everything, so there's that :/

closes #44 for real now tho
@OliverJAsh
Copy link
Author

Thanks for the fix @trxcllnt. I'll give it another go once it's released.

There's still the issue that 1 instanceof Number evaluates to false

I've updated the example in the original post now.

@OliverJAsh
Copy link
Author

OliverJAsh commented Nov 10, 2017

@trxcllnt Thanks for fixing this! My original example now works (ix 2.3.1), however this doesn't:

// no error, good!
const xs2: Ix.Iterable<number> = Ix.Iterable
    .from(['foo', 1])
    .filter((x): x is number => typeof x === 'number')

// no error, good!
const xs2b: Ix.Iterable<number> = xs2.map(x => x);

// unexpected error, bad!
const xs3: Ix.Iterable<number> = Ix.Iterable
    .from(['foo', 1])
    .filter((x): x is number => typeof x === 'number')
    .map(x => x)

Any ideas what is causing this?

@OliverJAsh
Copy link
Author

I opened a separate issue to track the above #162

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