Functional programming isn't my strong suit, but this should get you going.
import * as t from 'io-ts';
import { pipe } from 'fp-ts/function';
import { fold } from 'fp-ts/Either';
const Example = t.keyof({
foo: null,
bar: null,
});
type ExampleType = typeof Example;
function toExample(value: string) {
const exampleE = Example.decode(value);
const example = pipe(
exampleE,
fold(
(errors) => {
const reason = errors
.map((error) => `"${error.value}" does not match ${Example.name}`)
.join('\n');
throw new Error(reason);
},
(value) => value
)
);
return example;
}
const example = toExample('foo');
console.log('value=', example);
// Will throw error
const example2 = toExample('bad');