io-ts icon indicating copy to clipboard operation
io-ts copied to clipboard

from partial to require

Open waynevanson opened this issue 5 years ago • 2 comments

with the experimental codec API, how do we set default values for partials?

import * as codec from "io-ts/Codec";

interface In {
  folder?: boolean;
}

interface Out {
  folder: boolean;
}

// how to turn this to be Out type?
const innn = codec.partial({
  folder: codec.boolean
})

waynevanson avatar Sep 23 '20 03:09 waynevanson

You could use imap

import { pipe, identity } from 'fp-ts/function'

const c: codec.Codec<unknown, In, Out> = pipe(
  innn,
  codec.imap(({ folder }) => ({ folder: folder ?? false }), identity)
)

gcanti avatar Sep 23 '20 07:09 gcanti

Awesome. I did try this, but I what I'm really after is a combinator where the codec is used to.

I'll try out some kind and report back for the masses.

waynevanson avatar Sep 23 '20 07:09 waynevanson