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: Type is too long to infer, needs explicit type annotation. #308

Open
wembleyoz opened this issue Sep 19, 2024 · 4 comments

Comments

@wembleyoz
Copy link

Read before opening

  • Did you search the current list of issues ? Yes
  • Is your issue related to zod or zodios ? No
  • Are you using a Swagger v2 input schema ? No
  • Do you really need runtime validation ? Yes

Describe the bug
When generating the client from a large OpenAPI schema, such as the one at LCU OpenAPI schema, the following TypeScript error is encountered: The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.

The error seems to occur due to the size and complexity of the generated schema and type inference limits in TypeScript.

Minimal reproduction

Expected behavior
I expect the generated code to either handle larger schemas gracefully or suggest adding explicit type annotations to avoid the TypeScript inference error. The code should look like the example provided, which avoids importing large objects directly and instead exports each schema individually.

Additional context

  • Using OpenAPI 3.0 schema from LCU OpenAPI schema.
  • The issue occurs with large schemas and complex type inference in TypeScript. The error suggests using explicit type annotations for better control over the type size.

Suggested improvement
Instead of importing large schemas as a whole (schemas = {...all_schemas}), export individual schemas to avoid the same issue with TypeScript’s type inference limits.

Schematic example of the suggested code change:

import { makeApi, Zodios, type ZodiosOptions, ZodiosInstance } from "@zodios/core";
import { z } from "zod";

{{#if imports}}
{{#each imports}}
import { {{{@key}}} } from "./{{{this}}}"
{{/each}}
{{/if}}

{{#if types}}
{{#each types}}
{{{this}}};
{{/each}}
{{/if}}

{{#ifNotEmptyObj schemas}}
{{#each schemas}}
export const {{@key}}{{#if (lookup ../emittedType @key)}}: z.ZodType<{{@key}}>{{/if}} = {{{this}}};
{{/each}}
{{/ifNotEmptyObj}}

const endpoints = makeApi([
{{#each endpoints}}
	{
		method: "{{method}}",
		path: "{{path}}",
		{{#if @root.options.withAlias}}
		{{#if alias}}
		alias: "{{alias}}",
		{{/if}}
		{{/if}}
		{{#if description}}
		description: `{{description}}`,
		{{/if}}
		{{#if requestFormat}}
		requestFormat: "{{requestFormat}}",
		{{/if}}
		{{#if parameters}}
		parameters: [
			{
				name: "{{name}}",
				{{#if description}}
				description: `{{description}}`,
				{{/if}}
				type: "{{type}}",
				schema: {{{schema}}}
			},
			{{/each}}
		],
		response: {{{response}}},
		errors: [
			{
				status: {{status}},
				description: `{{description}}`,
				schema: {{{schema}}}
			}
		]
	},
{{/each}}
]);

export const apiClient: ZodiosInstance<typeof endpoints> = new Zodios(endpoints);

export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
    return new Zodios(baseUrl, endpoints, options);
}
@ChrisSargent
Copy link

We are also running in to this issue. Tried the above but we also get the errors in the createApiClient function and the const endpoints = makeApi.

Our only workaround I think right now is to group the endpoints. Any other guidance would be appreciated.

@wembleyoz
Copy link
Author

We are also running in to this issue. Tried the above but we also get the errors in the createApiClient function and the const endpoints = makeApi.

Our only workaround I think right now is to group the endpoints. Any other guidance would be appreciated.

You can just replace the variable apiClient with

export const apiClient: ZodiosInstance<typeof endpoints> = new Zodios(endpoints);

@ChrisSargent
Copy link

We are also running in to this issue. Tried the above but we also get the errors in the createApiClient function and the const endpoints = makeApi.
Our only workaround I think right now is to group the endpoints. Any other guidance would be appreciated.

You can just replace the variable apiClient with

export const apiClient: ZodiosInstance<typeof endpoints> = new Zodios(endpoints);

not for us, it just shifts the same error to the const endpoints line....

@alden12
Copy link

alden12 commented Nov 19, 2024

Same for us, will this be possible to fix without splitting up APIs or is it a limitation of TypeScript itself?

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

3 participants