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

Compiler issues with TypeScript 3.3.3, io-ts 1.8.1 and io-ts-types 0.4.2 #100

Closed torkelrogstad closed 5 years ago

torkelrogstad commented 5 years ago
import * as t from "io-ts";
import * as types from "io-ts-types";

const myType = t.type({
  date: types.DateFromISOString // this does not compile...
});

Complete code for this is at https://github.com/torkelrogstad/i-am-going-crazy

Any help appreciated!

gcanti commented 5 years ago

Please specify what's the issue and the steps required to repro

torkelrogstad commented 5 years ago

The steps required to reproduce is the versions of TypeScript, io-ts and io-ts-types listed above (also, the tsconfig.json in the repo I linked. I'd expect the above code to compile, and give me a type that validates the field date against the DateFromISOString type. Instead I get a compiler error.

What am I doing wrong?

gcanti commented 5 years ago

Instead I get a compiler error

@torkelrogstad What's the error?

If I clone your repo and run npx tsc i get

node_modules/io-ts-types/lib/fp-ts/createSetFromArray.d.ts:9:79 - error TS2583: Cannot find name 'Set'. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

9 export interface SetFromArrayC<C extends t.Mixed> extends SetFromArrayType<C, Set<t.TypeOf<C>>, Array<t.OutputOf<C>>, t.mixed> {

After adding the required options to lib in tsconfig.json

"lib": ["es2015", "dom"]

it compiles fine

torkelrogstad commented 5 years ago
❯ npx tsc
index.ts:6:3 - error TS2322: Type 'DateFromISOStringC' is not assignable to type 'Mixed'.
  Types of property 'pipe' are incompatible.
    Type '<B, IB, A extends IB, OB extends A>(this: Type<A, string, unknown>, ab: Type<B, OB, IB>, name?: string | undefined) => Type<B, string, unknown>' is not assignable to type '<B, IB, A extends IB, OB extends A>(this: Type<A, any, unknown>, ab: Type<B, OB, IB>, name?: string | undefined) => Type<B, any, unknown>'.
      The 'this' types of each signature are incompatible.
        Type 'Type<A, any, unknown>' is not assignable to type 'Type<IB, string, unknown>'.
          Types of property 'is' are incompatible.
            Type 'Is<A>' is not assignable to type 'Is<IB>'.
              Type predicate 'u is A' is not assignable to 'u is IB'.
                Type 'A' is not assignable to type 'IB'.
                  Type 'IB' is not assignable to type 'IB'. Two different types with this name exist, but they are unrelated.

6   date: types.DateFromISOString // this does not compile...
    ~~~~

  node_modules/io-ts/lib/index.d.ts:475:5
    475     [key: string]: Mixed;
            ~~~~~~~~~~~~~~~~~~~~~
    The expected type comes from this index signature.

Found 1 error.

This is after adding "lib": ["es2015", "dom"]

torkelrogstad commented 5 years ago

After diving into yarn.lock I found a fix for this issue.

https://github.com/torkelrogstad/i-am-going-crazy/commit/9d5b5d113fd2eaba253529ecfd2d96dcbbc7a894 this commit makes the error message go away. Curious if you are able to reproduce. rm -rf node_modules && yarn && npx tsc should give you an error message without that commit, no error message with it.

gcanti commented 5 years ago

Curious if you are able to reproduce

No, but I don't use yarn, maybe that's the difference

torkelrogstad commented 5 years ago

I think that's the difference. The yarn.lock file in the repo I linked causes yarn to download the exact dependencies listed in that file.