question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Builtin composite decoders cannot take decoders with non-`unknown` input types

See original GitHub issue

Version

2.0.0-beta9

Steps to reproduce

  1. Define a Decoder<O, I> where I is a type other than unknown
  2. 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:closed
  • Created 2 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
nviecommented, Feb 2, 2022

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 old compose 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.

1reaction
nviecommented, Jan 28, 2022

Hey @treykasada – I just published 2.0.0-beta10 which enables the examples above. Would you like to give it a spin? Much appreciated! 🙏

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found