gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

Wrong behavior of `mapOutput` #180

Closed martinschayna closed 1 year ago

martinschayna commented 1 year ago

🐛 Bug report

Current Behavior

What I'm doing wrong? I've tried several examples of mapOutput from documentation and resolved issues here, but all of them seams to be wrong too.

import * as t from "io-ts";
import { mapOutput } from "io-ts-types";

const uppercase = mapOutput(t.string, s => s.toUpperCase());
uppercase.decode("a"); // ? { _tag: "Right", right: "a" }

Expected behavior

uppercase.decode("a"); // ? { _tag: "Right", right: "A" }  see "A" instead of "a"

Suggested solution(s)

I've tried to put log into the function in second argument, but it was never called. It should be called when the codec in the first argument parses input.

Your environment

    "fp-ts": "2.14.0",
    "io-ts": "2.2.20",
    "io-ts-types": "0.5.19",
    "typescript": "5.0.2",
mlegenhausen commented 1 year ago

The documentation clearly states that mapOutput changes the return value of encode not from decode.

In your example: uppercase.encode("a") // ? "A"

martinschayna commented 1 year ago

Oh, I see, thank you. I am using io-ts mainly for parsing responses from API, I always use decode and obviously ignore the other side of codecs. My fault.