Skip to content

Lack of type safety for errors when mocking a 204 endpoint #2888

@not-my-profile

Description

@not-my-profile

The RequestResult type results in a lack of type safety when the return value of methods for 204 endpoints (where data: undefined).

Reproducible example or configuration

Create openapi.yaml with:

openapi: 3.1.0
paths:
  "/":
    get:
      operationId: example__get
      responses:
        '204': {}
        '404':
          content:
            application/json:
              schema:
                properties:
                  message:
                    type: string
                    title: Message
                type: object
                required:
                  - message

Generate the client with npx @hey-api/openapi-ts -i ./openapi.yaml -o src/client.

Create tsconfig.json with:

{
  "compilerOptions": {
    "lib": ["ESNext", "DOM"],
    "types": ["jasmine"]
  }
}

Run npm add @types/jasmine.

Create src/demo.ts with:

import * as client from "./client/sdk.gen";

spyOn(client, "exampleGet").and.returnValue(
  Promise.resolve({
    data: undefined,
    error: 123, // no type error :(
    request: new Request(""),
    response: new Response(),
  }),
);

Observe that the error value results in no type error even though it should. This is caused by the ThrowOnError extends true conditional type in the definition of the RequestResult type. Removing the conditional (or replacing ThrowOnError extends true with false extends true) makes the code produce the desired type error.

It would be nice if there was an option to not generate the ThrowOnError generics since we never use them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🔥Something isn't workingclientClient package related

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions