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

The inferred type of 'LogEntryEventCodec' cannot be named without a reference to '@huckleberryai/core/node_modules/io-ts-types/lib/optionFromNullable'. This is likely not portable. A type annotation is necessary. #110

Open dragosrotaru opened 5 years ago

dragosrotaru commented 5 years ago

πŸ› Bug report

Current Behavior

When I extend a Type from an external module, I get the error mentioned in the issue title.

import * as iots from "io-ts";
import { NonEmptyString } from "io-ts-types/lib/NonEmptyString";
import {  EventCodec } from "@huckleberryai/core";

... LogLevelCodec defined ...

export const LogEntryEventCodec = iots.intersection(
  [
    EventCodec,
    iots.type({
      message: NonEmptyString,
      level: LogLevelCodec,
      tags: iots.array(NonEmptyString)
    })
  ],
  LogEntryEventType
);

To fix the error, I have to add the following import:

import "io-ts-types/lib/optionFromNullable"

In my @huckleberryai/core package:

export const EventCodec = iots.type({
  timestamp: TimeStampCodec,
  type: TypeCodec,
  context: TypeCodec,
  origin: TypeCodec,
  id: UUIDCodec,
  corr: UUIDCodec,
  parent: optionFromNullable(UUIDCodec),
  agent: optionFromNullable(UUIDCodec),
});

Expected behavior

I expect that when I write a Codec that uses an io-ts-type, I can export that codec in our internal packages and import it without issues.

Suggested solution(s)

I'm not really sure how you would fix this. Would love for someone to explain to me what is going on

Additional context

I should also mention that

Your environment

Software Version(s)
fp-ts ^2.1.0
io-ts ^2.0.1
io-ts-types ^0.5.1
TypeScript ^3.5.3
apalumbo commented 3 years ago

@DragosRotaru I am not sure if you still have the issue. I just had a similar one in one project of mine. At the end it was related, as far as I have discovered, to the fact that the dependency io-ts-types (in your use case) is a dependency of your module (@huckleberryai/core). You should move it to a to dev dependency and add it also as a peer dependency. Once you add io-ts-types to your main project (the external module that is using @huckleberryai/core) it should work as expected.

dbernar1 commented 3 years ago

In my experience, the only thing that worked was to add export { OptionFromNullableC } from "io-ts-types"; to every place I had import "io-ts-types/lib/optionFromNullable"

HtH, Dan