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

Codec law does not hold for optionFromNullable #166

Open
OliverJAsh opened this issue Dec 14, 2021 · 1 comment
Open

Codec law does not hold for optionFromNullable #166

OliverJAsh opened this issue Dec 14, 2021 · 1 comment

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Dec 14, 2021

🐛 Bug report

Current Behavior

The following law as defined here:

pipe(codec.decode(u), E.fold(() => u, codec.encode)) = u for all u in unknown

import * as assert from "assert";
import * as E from "fp-ts/Either";
import { pipe } from "fp-ts/function";
import * as t from "io-ts";
import { optionFromNullable } from "io-ts-types";

const codec = optionFromNullable(t.string);

const u = undefined;

// ❌ This fails because the encode result is `null`, not `undefined`
assert.strictEqual(
    pipe(
        codec.decode(u),
        E.fold(() => u, codec.encode)
    ),
    u
);
AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:
+ actual - expected

+ null
- undefined

Expected behavior

Reproducible example

See above.

Suggested solution(s)

Currently we have optionFromNullable which decodes undefined | null | T and encodes to null | T.

Perhaps we could separate this into two codecs:

  • optionFromOptional: decodes undefined | T and encodes to undefined | T
  • optionFromNullable: decodes null | T and encodes to null | T

Additional context

Your environment

Which versions of io-ts-types are affected by this issue? Did this work in previous versions of io-ts-types?

Software Version(s)
fp-ts
io-ts
io-ts-types
TypeScript
@huntwj
Copy link

huntwj commented Jan 13, 2023

I see this is a year old, but I would also like to see this happen.

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

No branches or pull requests

2 participants