- [Codec interface](#codec-interface) # Codec interface ```ts export interface Codec extends D.Decoder, E.Encoder {} ``` A codec is just a decoder and an encoder packed together. The following laws must hold 1. `pipe(codec.decode(u), E.fold(() => u, codec.encode)) = u` for all `u` in `unknown` 2. `codec.decode(codec.encode(a)) = E.right(a)` for all `a` in `A` You can build a new codec using the `make` helper **Example** ```ts import * as C from 'io-ts/Codec' import * as D from 'io-ts/Decoder' import * as E from 'io-ts/Encoder' import { pipe } from 'fp-ts/function' const decoder: D.Decoder = pipe( D.string, D.parse((s) => { const n = parseFloat(s) return isNaN(n) ? D.failure(s, `cannot decode ${JSON.stringify(s)}, should be parsable into a number`) : D.success(n) }) ) const encoder: E.Encoder = { encode: String } export const NumberFromString: C.Codec = C.make(decoder, encoder) ```