Skip to content

Should "serialized" imply JSON for member codecs? #27

@OliverJAsh

Description

@OliverJAsh

In this example we are trying to compose the result of getCodecFromSerialized with JsonFromString. The error makes it difficult to spot which member codec is the culprit.

import * as Sum from '@unsplash/sum-types';
import { getCodecFromSerialized } from '@unsplash/sum-types-io-ts';
import * as t from 'io-ts';
import { JsonFromString } from 'io-ts-types';

type MyUnion =
  | Sum.Member<'A', string>
  | Sum.Member<'B', string>
  | Sum.Member<'C', string | undefined>;
const MyUnion = Sum.create<MyUnion>();

const Codec = getCodecFromSerialized(MyUnion)({
  A: t.string,
  B: t.string,
  C: t.union([t.string, t.undefined]),
});
const CodecFromString = JsonFromString.pipe(
  // Error: Type 'readonly ["A", string] | readonly ["B", string] | readonly ["C", string | undefined]' is not assignable to type 'Json'.
  Codec,
);

If getCodecFromSerialized required Json as the output type of all member codecs, we would have a much nicer error message:

const Codec = getCodecFromSerialized(MyUnion)({
  A: t.string,
  B: t.string,
  // Type 'undefined' is not assignable to type 'Json'.
  C: t.union([t.string, t.undefined]),
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions