Io-ts: Readonly by default

Created on 18 Oct 2020  路  3Comments  路  Source: gcanti/io-ts

As I understand, fp-ts 3 will be readonly only. Should experimental io-ts decode to readonly types as well?

experimental

Most helpful comment

@steida IMO yes, they definitely should.

All 3 comments

@steida IMO yes, they definitely should.

A first consideration: the following pattern (which can be used to clean a Decoder signature)

import * as D from 'io-ts/Decoder'

const _Person = D.type({
  name: D.string,
  age: D.number
})

interface Person extends D.TypeOf<typeof _Person> {}

const Person: D.Decoder<unknown, Person> = _Person

can be slightly modified to support readonly types

import * as D from 'io-ts/Decoder'

const _Person = D.type({
  name: D.string,
  age: D.number
})

interface Person extends Readonly<D.TypeOf<typeof _Person>> {}

const Person: D.Decoder<unknown, Person> = _Person

so we may not have to do anything.

The built-in Readonly type can also be used to define a readonly combinator

import { identity } from 'fp-ts/function'

export const readonly: <I, A>(decoder: D.Decoder<I, A>) => D.Decoder<I, Readonly<A>> = identity

const Person = readonly(
  D.type({
    name: D.string,
    age: D.number
  })
)

can be slightly modified to support readonly types
so we may not have to do anything.

It looks weird to define all models like that.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cyberixae picture cyberixae  路  3Comments

spacejack picture spacejack  路  5Comments

gabro picture gabro  路  4Comments

Kinrany picture Kinrany  路  5Comments

philipp-schaerer-lambdait picture philipp-schaerer-lambdait  路  3Comments