-
Notifications
You must be signed in to change notification settings - Fork 54
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
How to access .or
prop of decoders
#141
Comments
I would assume you could use static js-multiformats/src/bases/base.js Lines 165 to 175 in a4f998b
because b32.decoder should be compatible because it is js-multiformats/src/bases/base.js Lines 75 to 78 in a4f998b
js-multiformats/src/bases/base.js Line 206 in a4f998b
I think things do not work because of your type annotation here const bases:Record<string, MultibaseCodec<any>> = {
...b32,
...b36,
...b58,
...b64
} Could you just leave it out and let TS infer the type correctly ? |
I'll create pull request that adds a test with this |
No, because then this errors with .map(key => bases[key].decoder)
Nice, I didn't spot that. Unfortunately it's defined in If I patch that locally I can simplify a little with: import { bases } from 'multiformats/basics'
import { or } from 'multiformats/bases/base'
const baseDecoder = Object
.values(bases)
.map(codec => codec.decoder)
.reduce(or, bases.identity.decoder) But now passing
There's quite a lot going on here, could these types not be simplified a bit? |
I've investigated this further and issue is that TS fails to infer types properly, specifically it can't decide if it should treat first arg of reduce as Decoder or ComposedDecoder (which should not really matter for composition but it does for reduce). I have WIP patch that makes it all work but I'm not happy with they way it's looking: const bases = {
...b32,
...b36,
...b58,
...b64
}
const composite = Object
.values(bases)
.map(codec => codec.decoder.composed)
.reduce(b.or) With above code things work as expected, but |
@achingbrain is manually composing with For the context, thinking here was that likely users would use handful of codecs that they can compose together as opposed to large amount. Alternatively we could just expose a function like: export const fromTable = (decoders) => new CompositeDecoder(decoders) Which would then just accept your |
BTW when you use |
This seems a bit backwards. The I can do: import { bases } from 'multiformats/basics'
const baseDecoder = Object
.values(bases)
.map(codec => codec.decoder)
// @ts-expect-error `.or` is missing from the types
.reduce((acc, curr) => acc.or(curr), bases.identity.decoder) and everything works though obviously the type checker tells me it doesn't. Perhaps it could just be added to the |
I'd like to compose a decoder, something like:
It's not clear to me how I'm supposed to do this and not upset the type checker, any pointers?
I tried adding
.or
to theMultibaseDecoder
type def but it explodes because elsewhere you need to know if you're being passed aUnibaseDecoder
or aCombobaseDecoder
to create aComposedDecoder
- I started trying to fix it but it got a little out of hand so I thought I'd ask instead.The text was updated successfully, but these errors were encountered: