ts-rest: Basic example with express: Typescript fails with `Type instantiation is excessively deep and possibly infinite`

Hi friends, I am exploring ts-rest and hope not to use Zod directly, as I use io-ts for a lot of similar typing work. I am following the quickstart and have installed Zod as a dev dependency. Here are my relevant versions:

  "dependencies": {
    "@ts-rest/core": "^3.30.2",
    "@ts-rest/express": "^3.30.2",
    "express": "^4.18.2"
  },
  "devDependencies": {
    "@types/express": "^4.17.17",
    "typescript": "^4.9.5",
    "zod": "^3.22.2"
  },

However, when I contrive this basic example contract and server:

// contract.ts
const c = initContract()

export const contract = c.router({
  getInfo: {
    method: 'GET',
    path: '/info',
    responses: {
      200: c.type<ApiInfo>(),
    },
    summary: 'Get information about this Docmaps API server',
  },
})

// server.ts
  const s = initServer()

  const router = s.router(contract, {
    getInfo: async () => {
      const info = api.get_info()

      return {
        status: 200,
        body: info,
      }
    },
  })

  createExpressEndpoints(contract, router, app)        // <---- ERROR

I get this error on typescript compilation: Type instantiation is excessively deep and possibly infinite. typescript (2589)

Apparently this is an issue that occurs in certain uses of Zod, but since I don’t invoke Zod, i expect this might be an issue with how ts-rest calls Zod.

Can you help me understand if there is a known workaround for it while using ts-rest?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Thanks @Irelynx and @Talent30 . I also can confirm that downgrading Zod did not help, but downgrading @ts-rest/core to 3.28.0 did prevent the error. The version of @ts-rest/express does not appear to matter in either case.

I had a similar issue, but on the client side. I’m using a monorepo and share the contract in a separate package. During testin, initClient() worked fine when called inside the shared package, but errored on the client.

error TS2589: Type instantiation is excessively deep and possibly infinite
Argument of type '{ getConfig: { method: "GET"; path: "/getconfig"; responses: { 200: ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }; strictStatusCodes: true; }; sendChatMessage: { ...; }; sendDiscordMessage: { ....' is not assignable to parameter of type 'AppRouter'.
  Property 'getConfig' is incompatible with index signature.
    Type '{ method: "GET"; path: "/getconfig"; responses: { 200: ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }; strictStatusCodes: true; }' is not assignable to type 'AppRouter | AppRoute'.
      Type '{ method: "GET"; path: "/getconfig"; responses: { 200: ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }; strictStatusCodes: true; }' is not assignable to type 'AppRouteQuery'.
        Type '{ method: "GET"; path: "/getconfig"; responses: { 200: ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }; strictStatusCodes: true; }' is not assignable to type 'AppRouteCommon'.
          Types of property 'responses' are incompatible.
            Type '{ 200: ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>; }' is not assignable to type 'Record<number, AppRouteResponse>'.
              Property '200' is incompatible with index signature.
                Type 'ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'AppRouteResponse'.
                  Type 'ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>' is not assignable to type 'ZodType<any, ZodTypeDef, any>'.
                    The types returned by '_refinement(...)' are incompatible between these types.
                      Type 'ZodEffects<ZodObject<{ userChannelBlockOtherMessages: ZodBoolean; userChannelId: ZodNullable<ZodString>; canSendMessages: ZodBoolean; }, "strip", ZodTypeAny, { ...; }, { ...; }>, { ...; }, { ...; }>' is missing the following properties from type 'ZodEffects<ZodType<any, ZodTypeDef, any>, any, any>': _type, _output, _input, _def, and 33 more.

I solved it by downgrading zod to 3.25.10, which seems to be the latest working version. This happened completely at random for me and took some time to debug.