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

Inaccessible unique symbol error when using FetchErrorType #37

Open
htunnicliff opened this issue Jun 5, 2022 · 1 comment
Open

Inaccessible unique symbol error when using FetchErrorType #37

htunnicliff opened this issue Jun 5, 2022 · 1 comment

Comments

@htunnicliff
Copy link

htunnicliff commented Jun 5, 2022

When I attempt to use FetchErrorType<F>, TypeScript emits the following error:

The inferred type of makeHook1 references an inaccessible 'unique symbol' type. A type annotation is necessary.
ts(2527)

I investigated this and discovered that the following const declaration (on line 65) is the cause of the problem.

// private symbol to prevent narrowing on "default" error status
const never: unique symbol = Symbol()
type _OpErrorType<T> = {
[S in Exclude<keyof T, 200 | 201>]: {
status: S extends 'default' ? typeof never : S
data: T[S]
}
}[Exclude<keyof T, 200 | 201>]

If I modify that source code to export the type, the error I mentioned disappears with no further modifications needed in my own code:

// private symbol to prevent narrowing on "default" error status
-const never: unique symbol = Symbol()
+export const never: unique symbol = Symbol()

type _OpErrorType<T> = {
  [S in Exclude<keyof T, 200 | 201>]: {
    status: S extends 'default' ? typeof never : S
    data: T[S]
  }
}[Exclude<keyof T, 200 | 201>]

Would it be feasible to add an export to this constant to resolve the "inaccessible unique symbol" error here?

Footnotes

  1. makeHook is the name of the calling function in my code.

@htunnicliff htunnicliff changed the title TypeScript error TS25when using FetchErrorType<F> Inaccessible unique symbol error when using FetchErrorType Jun 5, 2022
@htunnicliff htunnicliff changed the title Inaccessible unique symbol error when using FetchErrorType Inaccessible unique symbol error when using FetchErrorType Jun 5, 2022
@htunnicliff
Copy link
Author

Here is an approximation of my calling code. The relevant part of the function is that it accepts an OpenAPI schema paths type in its generic, then lets the user enter one of the available path keys in order to generate a fetcher for the given path.

import { Fetcher } from "openapi-typescript-fetch";
import type { OpenapiPaths } from "openapi-typescript-fetch/dist/cjs/types";

type Gettable = {
  get: unknown;
};

type OnlyGet<T> = {
  [K in keyof T as T[K] extends Gettable ? K : never]: T[K];
};

export function makeHook<
  Paths extends OpenapiPaths<Paths>, 
  Path extends keyof OnlyGet<Paths>
>(path: Path) {
  // Create fetcher
  const fetcher = Fetcher.for<Paths>();
  fetcher.configure({});

  // Create request
  const request = fetcher.path(path).method("get").create();
  type Data = FetchReturnType<typeof request>;
  type Args = FetchArgType<typeof request>;
  type FetchError = FetchErrorType<typeof request>; // <-- TS2527 error

  // ... do other stuff to create and return a hook 
}

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

Successfully merging a pull request may close this issue.

1 participant