Closed EricCrosson closed 4 years ago
Thanks @EricCrosson, I would add an optional name
argument though:
export function withEncode<A, O, I, P>(
codec: t.Type<A, O, I>,
encode: (a: A) => P,
name: string = codec.name
): t.Type<A, P, I> {
return new t.Type(name, codec.is, codec.validate, encode)
}
Excellent! That's a much better implementation too. Thanks for the suggestion @gcanti , I've pushed these updates to the PR
withEncode
is similar towithValidate
:withEncode
clones an existing codec and overrides the existingencode
function with the givenencode
function. This can be useful when you want to uset.type
to combinatorially create codecs but want to leverage theencode
function to encode into a different type.