$ npm install io-ts-types
A collection of runtime types and combinators for use with io-ts
newtype-tsfromNewtypeGiven
import { Newtype, iso } from 'newtype-ts'
type Age = Newtype<'Age', number>
I want to define a runtime type whose derived type is
type Person = {
name: string
age: Age
}
Solution
import * as t from 'io-ts'
import { fromNewtype } from 'io-ts-types/lib/newtype-ts/fromNewtype'
const Person = t.interface({
name: t.string,
age: fromNewtype<Age>(t.Integer)
})
Usage example
import { iso } from 'newtype-ts'
import { Lens } from 'monocle-ts'
type Person = t.TypeOf<typeof Person>
const ageLens = Lens.fromProp<Person, 'age'>('age').composeIso(iso<Age>())
const sum = (a: number) => (b: number) => a + b
console.log(t.validate({ name: 'Giulio', age: 43 }, Person).map(ageLens.modify(sum(1))))
// => right({ name: 'Giulio', age: 44 })
© 2010 - cnpmjs.org x YWFE | Home | YWFE