import * as t from 'io-ts' ; import { either } from 'fp-ts/lib/Either' ; export const DateFromString = new t . Type < Date , string , unknown > ( 'DateFromString' , ( value ) : value is Date => value instanceof Date , ( valueToDecode , context ) => either . chain ( t . string . validate ( valueToDecode , context ) , ( value ) => { const decoded = new Date ( value ) ; return isNaN ( decoded . getTime ( ) ) ? t . failure ( valueToDecode , context ) : t . success ( decoded ) ; } ) , ( valueToEncode ) => valueToEncode . toISOString ( ) ) ;