gcanti / io-ts-types

A collection of codecs and combinators for use with io-ts
https://gcanti.github.io/io-ts-types/
MIT License
311 stars 40 forks source link

import(".../io-ts/lib/index").Decoder is not assignable to type import(".../io-ts/lib/Decoder").Decoder #149

Closed ingun37 closed 3 years ago

ingun37 commented 3 years ago

🐛 Bug report

Current Behavior

When I do

import * as iots from "io-ts";
import * as D from "io-ts/Decoder";
import { withFallback } from "io-ts-types/lib/withFallback";
const MaterialInfo = D.type({
  mapElement: D.type({
    uiID: withFallback(iots.number, -1).asDecoder()
  }),
...

I get Error

Type 'import("/Users/ingunjon/Desktop/ws/viewer/meshFactoryTS/node_modules/io-ts/lib/index").Decoder<unknown, number>' is not assignable to type 'import("/Users/ingunjon/Desktop/ws/viewer/meshFactoryTS/node_modules/io-ts/lib/Decoder").Decoder<unknown, number>'

Expected behavior

I expect Decoder from io-ts/lib/index and Decoder from io-ts/lib/Decoder to not conflict each other.

Reproducible example

Suggested solution(s)

Additional context

Your environment

Which versions of io-ts-types are affected by this issue? Did this work in previous versions of io-ts-types?

Software Version(s)
fp-ts 2.9.3
io-ts 2.2.13
io-ts-types 0.5.12
TypeScript 4.0.3
cdimitroulas commented 3 years ago

Is there a workaround for this?

ljani commented 3 years ago

Is there a workaround for this?

io-ts-types is using the old Decoder interface (exported from io-ts/index.ts). If you want to use the new, experimental Decoder interface (exported from io-ts/Decoder.ts, you need to implement most of (all of?) io-ts-types yourself, or switch back to using the old interface.

gcanti commented 3 years ago

you could define an adapter, here's a (simplistic) solution

import * as t from 'io-ts'
import * as D from 'io-ts/Decoder'
import { pipe } from 'fp-ts/function'
import * as E from 'fp-ts/Either'

export function toDecoder<A, O, I>(type: t.Type<A, O, I>): D.Decoder<I, A> {
  const decode = type.decode.bind(type)
  return {
    decode: (i) =>
      pipe(
        decode(i),
        E.mapLeft(() => D.error(i, type.name))
      )
  }
}

import { withFallback } from 'io-ts-types/withFallback'

export const MaterialInfo = D.type({
  mapElement: D.type({
    uiID: toDecoder(withFallback(t.number, -1))
  })
})