Skip to content

Latest commit

 

History

History
44 lines (32 loc) · 996 Bytes

Encoder.md

File metadata and controls

44 lines (32 loc) · 996 Bytes

Encoder interface

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))
  }
}

Extracting static types from encoders

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
*/