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

Typescript error when returning ZodError from loader or action #6693

Closed
1 task done
pioluk opened this issue Jun 26, 2023 · 9 comments · Fixed by #6736
Closed
1 task done

Typescript error when returning ZodError from loader or action #6693

pioluk opened this issue Jun 26, 2023 · 9 comments · Fixed by #6736

Comments

@pioluk
Copy link

pioluk commented Jun 26, 2023

What version of Remix are you using?

1.18.0

Are all your remix dependencies & dev-dependencies using the same version?

  • Yes

Steps to Reproduce

Similar to #6558, but since 1.18.0 happens with ZodError.

Repro: TS Playground

Expected Behavior

No typescript error

Actual Behavior

Error: Type instantiation is excessively deep and possibly infinite.

@Jackardios
Copy link

Jackardios commented Jun 26, 2023

I am getting the same error when trying to get recursive category from loader (since remix@1.18.0)

export interface Category {
  children?: Array<Category>;
}

export async function loader({ request, params, context }: LoaderArgs) {
  const category: Category = {...};
  return json({ category })
}

export default function Page() {
  const { category } = useLoaderData<typeof loader>();
  //       ^ Error: Type instantiation is excessively deep and possibly infinite.ts(2589)
}

@sergiodxa
Copy link
Member

This happens because if the type is recursive and has no end it will be trying to convert it to JSON-compatible forever, it happens the same if you do:

let object = {}
object.key = object
JSON.stringify(object) // throws TypeError: Converting circular structure to JSON

I imagine ZodError has some circular dependency.

@Jackardios
Copy link

Jackardios commented Jun 26, 2023

Is there a workaround for recursive types (without changing those types)?

@ivogt
Copy link
Contributor

ivogt commented Jun 27, 2023

Many thanks for the Remix team and the great work on 1.18. However, I am also experiencing this with 50+ definitions for Salesforce commerce apis which prevents us upgrading to stable dev.

I am really hoping we can address that.

Thank you.

@daviddelusenet
Copy link

I'm running into this issue with types coming from the Shopify Hydrogen package.

@frontendphil
Copy link

FWIW, I'm running into a very similar issue. However, it only appears with V2_MetaArgs and not with useLoaderData. Has worked fine before. We also have a recursive type from a third-party library (name JSONContent from prose-mirror.

@AndKiel
Copy link

AndKiel commented Jun 30, 2023

I've got the same issue with types generated from graphql queries via graphql-codegen because some types in my API are recursive.

const { results, variables } = useLoaderData<ILoaderData>();
//                             ^error TS2589: Type instantiation is excessively deep and possibly infinite.

An ugly workaround is to do this:

const { results, variables } = useLoaderData() as unknown as ILoaderData;

It stops complaining and we still have the typings, even though they are not SerializeFrom.

Worked fine on 1.17.1.

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.18.1-pre.2 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@github-actions
Copy link
Contributor

🤖 Hello there,

We just published version 1.18.1 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants