gcanti/io-ts

TypeScript 4.7 beta compile errors

joeltg opened this issue ยท 4 comments

๐Ÿ› Bug report

This is a separate issue from #644, not related to ESM compatibility (as far as I can tell).

Current Behavior

io-ts fails to compile under the new TypeScript 4.7 beta.

Expected behavior

io-ts compiles without emitting any errors.

Reproducible example

package.json:

{
  "name": "example",
  "scripts": {
    "build": "tsc"
  },
  "dependencies": {
    "fp-ts": "^2.12.1",
    "io-ts": "^2.2.16"
  },
  "devDependencies": {
    "typescript": "^4.7.0-beta"
  }
}

tsconfig.json:

{ 
  "compilerOptions": {
    "strict": true
  }
}

index.ts:

import * as t from "io-ts"

const thing = t.type({ foo: t.string })
console.log(thing.is(5))
console.log(thing.is({ foo: "wow" }))
% npm run build

> example@0.0.0 build
> tsc

node_modules/io-ts/lib/index.d.ts:1387:29 - error TS2344: Type 'O' does not satisfy the constraint 'Exact<O, O>'.
  Type 'O' is not assignable to type '{ [K in ({ [K in keyof O]: K; } & { [K in keyof O]: never; } & { [key: string]: never; })[keyof O]]?: undefined; }'.

1387   OO extends Exact<O, OO> = O,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1383:34
    1383 export declare function alias<A, O, P, I>(
                                          ~
    This type parameter probably needs an `extends object` constraint.

node_modules/io-ts/lib/index.d.ts:1388:29 - error TS2344: Type 'P' does not satisfy the constraint 'Exact<P, P>'.
  Type 'P' is not assignable to type '{ [K in ({ [K in keyof P]: K; } & { [K in keyof P]: never; } & { [key: string]: never; })[keyof P]]?: undefined; }'.

1388   PP extends Exact<P, PP> = P,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1383:37
    1383 export declare function alias<A, O, P, I>(
                                             ~
    This type parameter probably needs an `extends object` constraint.

node_modules/io-ts/lib/index.d.ts:1395:29 - error TS2344: Type 'O' does not satisfy the constraint 'Exact<O, O>'.
  Type 'O' is not assignable to type '{ [K in ({ [K in keyof O]: K; } & { [K in keyof O]: never; } & { [key: string]: never; })[keyof O]]?: undefined; }'.

1395   OO extends Exact<O, OO> = O,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1391:34
    1391 export declare function alias<A, O, P, I>(
                                          ~
    This type parameter probably needs an `extends object` constraint.

node_modules/io-ts/lib/index.d.ts:1396:29 - error TS2344: Type 'P' does not satisfy the constraint 'Exact<P, P>'.
  Type 'P' is not assignable to type '{ [K in ({ [K in keyof P]: K; } & { [K in keyof P]: never; } & { [key: string]: never; })[keyof P]]?: undefined; }'.

1396   PP extends Exact<P, PP> = P,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1391:37
    1391 export declare function alias<A, O, P, I>(
                                             ~
    This type parameter probably needs an `extends object` constraint.

node_modules/io-ts/lib/index.d.ts:1403:29 - error TS2344: Type 'O' does not satisfy the constraint 'Exact<O, O>'.
  Type 'O' is not assignable to type '{ [K in ({ [K in keyof O]: K; } & { [K in keyof O]: never; } & { [key: string]: never; })[keyof O]]?: undefined; }'.

1403   OO extends Exact<O, OO> = O,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1399:34
    1399 export declare function alias<A, O, P, I>(
                                          ~
    This type parameter probably needs an `extends object` constraint.

node_modules/io-ts/lib/index.d.ts:1404:29 - error TS2344: Type 'P' does not satisfy the constraint 'Exact<P, P>'.
  Type 'P' is not assignable to type '{ [K in ({ [K in keyof P]: K; } & { [K in keyof P]: never; } & { [key: string]: never; })[keyof P]]?: undefined; }'.

1404   PP extends Exact<P, PP> = P,
                                 ~

  node_modules/io-ts/lib/index.d.ts:1399:37
    1399 export declare function alias<A, O, P, I>(
                                             ~
    This type parameter probably needs an `extends object` constraint.


Found 6 errors in the same file, starting at: node_modules/io-ts/lib/index.d.ts:1387

Suggested solution(s)

The errors show a couple suggestions but I don't understand them very well.

Additional context

Your environment

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

Software Version(s)
io-ts 2.2.16
fp-ts 2.12.1
TypeScript 4.7.0-beta
lkj4 commented

@gcanti do you have time to look into this?

@joeltg @lkj4 I can't reproduce with typescript@4.7.3

There is another 4.7 related error in #644 Not sure if they are related, but with 4.7 came some changes/improvements to the type inference, so there might be a connection.

It works in TypeScript 4.7.4 but it does not work in: 4.8.0-beta, 4.8.1-rc, and 4.8.2. So it's again broken in the latest TypeScript 4.8.