Io-ts: Typescript <3.6 incompatibility

Created on 6 Jan 2020  路  3Comments  路  Source: gcanti/io-ts

馃悰 Bug report

Current Behavior

When using the latest io-ts version 2.0.2 and typescript version <3.6 the tsc build fails with the error:

ERROR in ../../node_modules/io-ts/lib/index.d.ts:416:9 - error TS1086: An accessor cannot be declared in an ambient context.

416     get type(): C;

But the documentation mentions that typescript 3.5.2+ is supported.
The error seems to occur when a library is being built with Typescript 3.7+ which uses a getter or setter and the main project has a Typescript <3.6 dependency.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#class-field-mitigations

This is even more a problem while using angular 8.2.x because of the typescript <3.6 dependency.

Expected behavior

The io-ts definition files should be compatible with 3.5.2 or the documentation should be updated.

Reproducible example

I've created a test repository to test this behavior:
https://github.com/Lambda-IT/io-ts-test

Suggested solution(s)

Perhaps it is possible to build the index.d.ts file in a way which is compatible with projects which use typescript version >=3.5.x
Alternatively the documentation could be updated and for angular 8.2 users a typescript update to 3.6+ together with an disableTypeScriptVersionCheck setting could help.

Environment

| Software | Version(s) |
| ---------- | ---------- |
| io-ts | 2.0.3 |
| fp-ts | 2.3.0 |
| TypeScript | 3.5.3 |

bug

All 3 comments

@philipp-schaerer-lambdait thanks for the detailed report

Perhaps it is possible to build the index.d.ts file in a way which is compatible with projects which use typescript version >=3.5.x

Looks like the diff is

// index.d.ts

get type(): C; // <= [email protected] output
readonly type: C; // <= [email protected],3.6.x output

We could force the same output by adding the getter "by hand", check this PR out, what do you think?

The changes work and the build runs now correctly with the angular 8.2 Project. If the change from private runDefinition: () => C to public runDefinition: () => C is ok for the library code then I don't have any objections to merge this change.

@philipp-schaerer-lambdait patch released, thanks for reviewing

Was this page helpful?
0 / 5 - 0 ratings