export interface Encoder<O, A> {
readonly encode: (a: A) => O
}
Example
An encoder representing a nullable value
import * as E from 'io-ts/Encoder'
export function nullable<O, A>(or: E.Encoder<O, A>): E.Encoder<null | O, null | A> {
return {
encode: (a) => (a === null ? null : or.encode(a))
}
}
Static types can be extracted from encoders using the OutputOf
operator
const NumberToString: E.Encoder<string, number> = {
encode: String
}
type MyOutputType = E.OutputOf<typeof NumberToString>
/*
type MyOutputType = string
*/