Inferred Types Cause Duplicated Inline HTTP Schemas In Type Declarations

by ADMIN 73 views

Introduction

In the context of type declarations, inferred types can sometimes lead to duplicated inline HTTP schemas. This issue arises when using the @zimic/fetch library, which is designed to simplify the process of working with HTTP requests and responses in TypeScript. In this article, we will delve into the problem of duplicated inline HTTP schemas in type declarations and explore the expected behavior.

Scope

The issue described in this article is specific to the @zimic/fetch library. It is essential to note that this problem does not affect other libraries or frameworks.

Prerequisites

Before we proceed, ensure that you have the following prerequisites:

  • None of the open issues on the @zimicjs/zimic repository match your problem.
  • The documentation for @zimic/fetch does not cover a solution to this problem.

System Information

The following system information is relevant to this issue:

  • System: Linux 5.0 undefined
  • CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
  • Memory: 0 Bytes / 0 Bytes
  • Shell: 1.0 - /bin/jsh
  • Binaries:
    • Node: 20.19.1 - /usr/local/bin/node
    • Yarn: 1.22.19 - /usr/local/bin/yarn
    • npm: 10.8.2 - /usr/local/bin/npm
    • pnpm: 8.15.6 - /usr/local/bin/pnpm
  • npm Packages:
    • @zimic/fetch: latest => 0.2.2
    • @zimic/http: latest => 0.3.1

Runtime

The issue described in this article is specific to the Node.js runtime environment.

Reproduction Link

A reproduction link is available on StackBlitz, which demonstrates the issue:

https://stackblitz.com/edit/github-mrf9afqh?file=src%2Fclients%2Fgithub.ts

Reproduction Steps

To reproduce the issue, follow these steps:

  1. Run pnpm install
  2. Run pnpm build
  3. Open dist/index.d.ts

Current Behavior

The current behavior of @zimic/fetch is to inline the HTTP schema instead of referencing it. This results in duplicated types in the declaration output.

declare function getGitHubRepository1(owner: string, name: string): Promise<{
    repository: {
        id: number;
        name: string;
        full_name: string;
        html_url: string;
        owner: {
            login: string;
        };
    };
    response: _zimic_fetch.FetchResponsePerStatusCode<HttpSchema.AsStrict<{
        '/repos/:owner/:name': {
            GET: {
                response: {
                    200: {
                        body: GitHubRepository;
                    };
                    403: {
                        body: {
                            message: string;
                        };
                    };
                    404: {
                        body: {
                            message: string;
                        };
                    };
                };
 };
        };
    }>, "GET", "/repos/:owner/:name", 200>;
}>;

Expected Behavior

The expected behavior of @zimic/fetch is to reference the schema instead of inlining it everywhere it is used. The expected output would be:

declare function getGitHubRepository1(owner: string, name: string): Promise<{
    repository: GitHubRepository;
    response: _zimic_fetch.FetchResponsePerStatusCode<GitHubSchema, "GET", "/repos/:owner/:name", 200>;
}>;

Conclusion

Q: What is the issue with inferred types in @zimic/fetch?

A: The issue arises when the inferred return types do not reference the GitHubSchema, instead inlining it everywhere it is used. This results in duplicated types in the declaration output.

Q: What is the expected behavior of @zimic/fetch?

A: The expected behavior is for the library to reference the schema instead of inlining it everywhere it is used. This would result in a cleaner and more maintainable type declaration output.

Q: How can I reproduce the issue?

A: To reproduce the issue, follow these steps:

  1. Run pnpm install
  2. Run pnpm build
  3. Open dist/index.d.ts

Q: What is the current behavior of @zimic/fetch?

A: The current behavior of @zimic/fetch is to inline the HTTP schema instead of referencing it. This results in duplicated types in the declaration output.

Q: What is the expected output of @zimic/fetch?

A: The expected output would be:

declare function getGitHubRepository1(owner: string, name: string): Promise<{
    repository: GitHubRepository;
    response: _zimic_fetch.FetchResponsePerStatusCode<GitHubSchema, "GET", "/repos/:owner/:name", 200>;
}>;

Q: How can I resolve this issue?

A: To resolve this issue, you can try the following:

  1. Update to the latest version of @zimic/fetch.
  2. Check the documentation for any changes or updates related to this issue.
  3. Reach out to the @zimicjs/zimic community for support and guidance.

Q: Is this issue specific to @zimic/fetch?

A: Yes, this issue is specific to the @zimic/fetch library. It is essential to note that this problem does not affect other libraries or frameworks.

Q: Can I report this issue to the @zimicjs/zimic repository?

A: Yes, you can report this issue to the @zimicjs/zimic repository. Make sure to follow the guidelines for reporting issues and provide a clear and concise description of the problem.

Q: How can I stay up-to-date with the latest developments on this issue?

A: To stay up-to-date with the latest developments on this issue, you can:

  1. Follow the @zimicjs/zimic repository on GitHub.
  2. Join the @zimicjs/zimic community for support and guidance.
  3. Check the documentation for any updates or changes related to this issue.