Example API spec doesn't compile w/ io-ts 2.2+
Closed this issue ยท 9 comments
I get a handful of errors just trying to compile the API spec provided in the examples.
src/api.ts:7:3 - error TS2322: Type 'Type<never, { query: { [x: number]: any; [x: string]: any; }; params: { [x: number]: any; [x: string]: any; }; headers?: {} | undefined; body?: {} | undefined; }, unknown>' is not assignable to type 'HttpRequestCodec<any>'.
Types of property 'encode' are incompatible.
Type 'Encode<never, { query: { [x: number]: any; [x: string]: any; }; params: { [x: number]: any; [x: string]: any; }; headers?: {} | undefined; body?: {} | undefined; }>' is not assignable to type 'Encode<any, { params: { [x: string]: string; }; query: { [x: string]: string | string[]; }; } & { headers?: { [x: string]: string; } | undefined; body?: Json | undefined; }>'.
Type 'any' is not assignable to type 'never'.
7 request: httpRequest({
~~~~~~~
../../node_modules/@api-ts/io-ts-http/dist/src/httpRoute.d.ts:14:14
14 readonly request: HttpRequestCodec<any>;
~~~~~~~
The expected type comes from property 'request' which is declared here on type 'HttpRoute<"get" | "post" | "put" | "delete">'
src/api.ts:9:7 - error TS2322: Type 'StringC' is not assignable to type '"Codec's output type is not assignable to `string | undefined`. Try using one like `NumberFromString`"'.
9 name: t.string,
~~~~
src/api.ts:13:5 - error TS2322: Type 'StringC' 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'.
'IB' could be instantiated with an arbitrary type which could be unrelated to 'A'.
13 200: t.string,
~~~
../../node_modules/@api-ts/io-ts-http/dist/src/httpResponse.d.ts:3:5
3 [K: number | string]: t.Mixed;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The expected type comes from this index signature.
src/api.ts:19:5 - error TS2322: Type 'HttpRoute<"get" | "post" | "put" | "delete">' is not assignable to type 'HttpRoute<"get">'.
Type '"get" | "post" | "put" | "delete"' is not assignable to type '"get"'.
Type '"post"' is not assignable to type '"get"'.
19 get: GetHelloWorld,
~~~
../../node_modules/@api-ts/io-ts-http/dist/src/httpRoute.d.ts:30:32
30 [ApiAction in keyof Spec]: {
~
31 [M in keyof Spec[ApiAction]]: M extends Method ? Spec[ApiAction][M] : `Unsupported HTTP Method. Use "get" | "post" | "put" | "delete"`;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32 };
~~~~~
The expected type comes from property 'get' which is declared here on type '{ get?: HttpRoute<"get"> | undefined; post?: HttpRoute<"post"> | undefined; put?: HttpRoute<"put"> | undefined; delete?: HttpRoute<"delete"> | undefined; }'
I'm using:
"@api-ts/io-ts-http": "^2.3.0"
"io-ts": "^2.2.20"
"@types/node": "^18.15.11"
typescript": "^5.0.4"
Cool repo btw, it's a useful library.
Thanks for opening this issue, @sangaman! It looks like our documentation has not kept up with some of the API changes since it was written -- egg on my face!
I hope it's not blocking you, but if you're still looking for some good, compiling examples, I can recommend the test cases as a starting point.
The docs actually look good and they line up with what's in the tests.
I believe I figured out the issue though, it's that I'm using an updated version of io-ts
. Looks like some breaking changes sneaked in from 2.1 to 2.2.
If you go to the express-wrapper
package and do npm i io-ts@latest
you'll see that the tests will start failing with compilation errors.
And likewise if I go to my project and do npm i io-ts@2.1.3
all my errors go away. So looks like io-ts 2.2+ is just not supported.
I suppose actually I should just not have io-ts as a dependency at all? It should just be brought in as a sub-dependency of api-ts? The reason I ran into this is because I was originally working just with io-ts so I had that installed and then tried to integrate this library as well, which didn't play nice with what was already there.
So looks like io-ts 2.2+ is just not supported
That's correct, the experimental io-ts versions are not supported. I don't know if that's mentioned prominently on the readme! I can open a PR to fix that
I suppose actually I should just not have io-ts as a dependency at all?
That's one way to do it! Since io-ts@2.1.3 is declared as a dependency of io-ts-http --
api-ts/packages/io-ts-http/package.json
Line 24 in dcd80f6
-- that same version will be present in your node_modules. I usually include io-ts@2.1.3 in my package.json anyway, to be explicit about what version my package depends on, but I suppose that's a personal preference.
๐ This issue has been resolved in version @api-ts/express-wrapper@1.0.20 ๐
The release is available on npm package (@latest dist-tag)
Your semantic-release bot ๐ฆ๐
๐ This issue has been resolved in version @api-ts/openapi-generator@2.0.0 ๐
The release is available on npm package (@latest dist-tag)
Your semantic-release bot ๐ฆ๐
๐ This issue has been resolved in version @api-ts/typed-express-router@1.1.1 ๐
The release is available on npm package (@latest dist-tag)
Your semantic-release bot ๐ฆ๐
๐ This issue has been resolved in version @api-ts/superagent-wrapper@1.1.12 ๐
The release is available on npm package (@latest dist-tag)
Your semantic-release bot ๐ฆ๐
๐ This issue has been resolved in version @api-ts/io-ts-http@2.4.1-beta.1 ๐
The release is available on @api-ts/io-ts-http
Your semantic-release bot ๐ฆ๐