Skip to content

Should MaybeOptionalInit really be an Array type? #1908

Closed

Description

@smaspe

Description

In the type definition of ClientMethod, init parameter is a rest parameter (...init), and its type is MaybeOptionalInit<Paths[P], M>, which is an Array.

However, when this type is used, in createClient, only the first element is ever used in the return value.

For example:

    /** Call a POST endpoint */
    POST(url, init) {
      return coreFetch(url, { ...init, method: "POST" });
    },

This is because init in the implementation of POST (and of all the other methods) is not a rest parameter, and so it only captures the first value.

The MaybeOptionalInit is actually always an array of 1 element (and the type is defined as such). However, when the type is captured, it's not obvious in all contexts.

Reproduction

It's a type issue, so it nothing happens at runtime.

Expected result

MaybeOptionalInit looks like it could be a simple value, not an Array. It would make it easier to capture the types of the parameters when wrapping the Client object into a decorator.

Checklist

Activity

  1. added
    bugSomething isn't working
    openapi-fetchRelevant to the openapi-fetch library
    on Sep 12, 2024
  2. drwpow commented on Sep 12, 2024

    @drwpow
    Contributor

    See this comment here: the array is just a type trick to determine whether or not the 2nd param is required for GET requests:

    // - Determines if the param is optional or not.

    It’s basically an alternate method to declaring function overloads for GET, which would result in more complex inference. It’s not actually an array; it’s just a way for TS to not require you putting undefined as the 2nd param.

    I’m not sure what the issue is; this seems more like a discussion so I’ll move it there. But If I’m missing something please let me know! And/or providing a failing test would be helpful.

  3. locked and limited conversation to collaborators on Sep 12, 2024
  4. converted this issue into a discussion #1909 on Sep 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions