Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic example with express: Typescript fails with Type instantiation is excessively deep and possibly infinite #389

Closed
ships opened this issue Sep 11, 2023 · 12 comments

Comments

@ships
Copy link

ships commented Sep 11, 2023

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?

@ships
Copy link
Author

ships commented Sep 11, 2023

I also tried this with removing zod as an additional dependency, and with it in prod dependencies rather than dev; but the error persists.

@Talent30
Copy link

Talent30 commented Sep 13, 2023

Please downgrade to zod 3.21.4

@ships
Copy link
Author

ships commented Sep 13, 2023

Thanks for the suggestion! After this downgrade I am still seeing the issue.

@Talent30
Copy link

Talent30 commented Sep 14, 2023

Thanks for the suggestion! After this downgrade I am still seeing the issue.

We had the same issue, with zod 3.22.x, could you try

a) downgrade ALL zod packages version if you are using monorepo and then
b) delete your node_modules folder and
c) npm i
d) if above didn not work please delete package-lock.json as well and then run npm i

This is how we solved it.

Something might be related colinhacks/zod#2697

@Irelynx
Copy link

Irelynx commented Sep 21, 2023

Hi. I have the same problem after updating ts-rest library.
Downgrading zod doesn't help in my situation, but downgrading @ts-rest/core to 3.28.0 fixes the issue (with [email protected])

The problem starts to appear in version @ts-rest/core 3.30.0

@ships
Copy link
Author

ships commented Sep 21, 2023

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.

@Talent30
Copy link

Talent30 commented Oct 4, 2023

Hey @ships. Looks like it is working with typescript 5.3 beta

@michaelangeloio
Copy link
Contributor

@Talent30 @ships @Irelynx is this still an issue? does anyone have a minimal reproduction repo?

@Talent30
Copy link

Talent30 commented Oct 8, 2023

@Talent30 @ships @Irelynx is this still an issue? does anyone have a minimal reproduction repo?

Hi, @michaelangeloio. I can’t reproduce it now.

@ships
Copy link
Author

ships commented Oct 13, 2023

I haven't had a chance to upgrade Typescript but will report back if that solves the issue, it should come in a week or two on this project.

@miku86
Copy link
Contributor

miku86 commented Nov 2, 2023

@michaelangeloio

I was able to solve it by:

  1. updating typescript from 5.2.2 to 5.3.0-beta
  2. removing package-lock.json
  3. removing node_modules
  4. running npm install

@ships
Copy link
Author

ships commented Nov 13, 2023

Okay, i finally got round to this typescript-5 upgrade and can confirm that as of ts-5.2.2 I no longer have this issue. I didn't even need to upgrade to typescript 5.3 beta.

@Gabrola Gabrola closed this as not planned Won't fix, can't repro, duplicate, stale Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants