[Bug] Builtin composite decoders cannot take decoders with non-`unknown` input types
See original GitHub issueVersion
2.0.0-beta9
Steps to reproduce
- Define a
Decoder<O, I>
whereI
is a type other thanunknown
- Attempt to pass the defined
Decoder<O, I>
to any of the builtin composite decoders
E.g.
import { Decoder, DecodeResult, optional } from 'decoders';
import { unknown } from 'decoders/annotate';
import { err, ok } from 'decoders/result';
import * as uuid from 'uuid';
const binaryUUIDFromStringUUID: Decoder<Uint8Array, string> = (uuidString: string): DecodeResult<Uint8Array> => {
try {
return ok(Uint8Array.from(uuid.parse(uuidString)));
} catch (error) {
return err(unknown(error));
}
};
const optionalBinaryUUIDFromStringUUID = optional(binaryUUIDFromStringUUID);
Expected result
No type errors.
Actual result
TS2345: Argument of type 'Decoder<Uint8Array, string>' is not assignable to parameter of type 'Decoder<Uint8Array, unknown>'.
Type 'unknown' is not assignable to type 'string'.
Affected composite decoders/functions:
guard
compose
predicate
transform
array
nonEmptyArray
set
describe
dict
exact
inexact
mapping
object
either
lazy
maybe
nullable
optional
taggedUnion
tuple
Motivating use-cases
- The binary UUID example from above
- Decoding arrays with a known element type without having to re-assert the known type:
const bigIntFromString: Decoder<bigint, string> = ...; const bigIntArrayFromStringArray: Decoder<bigint[], string[]> = array(bigIntFromString);
- Decoding objects with known value types without having to re-assert the known types:
const booleanFromString: Decoder<boolean, string> = ...; const bigIntFromString: Decoder<bigint, string> = ...; const recordFromStringifiedValueRecord: Decoder<{ foo: boolean, bar: bigint }, { foo: string, bar: string }> = object({ foo: booleanFromString, bar: bigIntFromString });
- Piece-wise composition of decoders:
const bigIntFromString: Decoder<bigint, string> = ...; const bigIntFromJSON: Decoder<bigint> = compose(compose(json, string), bigIntFromString);
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Full Text Bug Listing - Bugs - Eclipse
I've been using the RAW encoder/decoder successfully to describe several protocols that don't have a formal notation in order to use them from...
Read more >Construction of biocomputing systems based on switchable ...
Biocomputing, as a subfield of molecular computing, is a type of ... (B) A 1-to-2 decoder based on CP-free MIP film electrodes with...
Read more >Distributed, Embedded and Real-time Java Systems
Use in connection with any form of information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now ...
Read more >A discrete event approach to diagnosis of continuous systems
Abstract: A method for active fault diagnosis of linear discrete time hybrid systems is presented. The algorithm generates appropriate test signals that can...
Read more >Deep Learning with JavaScript - DOKUMEN.PUB
An LSTM-based encoder-attention- decoder architecture achieves a lower word-error rate than the best non-deep- learning speech recognition system.f.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yeah, I confess the beta iteration cycles have been a bit wild 🙈 But I think the end result is much nicer and more future-proof so it’s done mostly to avoid another major breaking change soon.
Btw, regarding your specific use of
compose
, there also is the.then()
method now which most closely resembles the oldcompose
helper. (But I still think for the aforementioned cases you won’t need it.)Please do let me know if you need help converting anything in particular that isn’t clearly documented enough – I much appreciate the early feedback and I’d be happy to help you out with anything.
Hey @treykasada – I just published
2.0.0-beta10
which enables the examples above. Would you like to give it a spin? Much appreciated! 🙏