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

Errors in curried function with overloads in JsDoc #49708

Closed
timonson opened this issue Jun 28, 2022 · 6 comments
Closed

Errors in curried function with overloads in JsDoc #49708

timonson opened this issue Jun 28, 2022 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@timonson
Copy link

The following function with overloads with JsDoc throws two errors. Written in pure TypeScript it passes the compiler. Is there a way to do this in JsDoc?
If there is no way, can I expect a fix in the future maybe or is it just technically impossible at the moment?
Thanks!

/**
 * @type {{
 *   (init: "a"): () => string;
 *   (init: "b"): () => number;
 * }}
 */
export const foo = (init) => {
  return () => {
    switch (init) {
      case "a":
        return "abc";
      case "b":
        return 10;
      default:
        throw Error("Invalid body method.");
    }
  };
};

console.log(foo("a")());

2322 [ERROR]: Type '(init: "a" | "b") => () => string' is not assignable to type '{ (init: "a"): () => string; (init: "b"): () => number; }'.
Call signature return types '() => string' and '() => number' are incompatible.
Type 'string' is not assignable to type 'number'.
export const handleResponse = (init) => {
~~~~~~~~~~~~~~

TS2322 [ERROR]: Type '() => 10 | "abc"' is not assignable to type '() => string'.
Type 'string | number' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
return () => {

@timonson timonson added the Duplicate An existing issue was already created label Jun 28, 2022
@MartinJohns
Copy link
Contributor

@timonson
Copy link
Author

timonson commented Jun 28, 2022

@MartinJohns it already works in pure TypeScript like I said in OP. So I am not sure why your link matters to my issue. I am asking about an error which occurs in JsDoc but which doesn't happen in TypeScript.

@MartinJohns
Copy link
Contributor

it already works in pure TypeScript like I said in OP

It does not: Playground link

@timonson
Copy link
Author

timonson commented Jun 28, 2022

OK, I didn't want to make my question about arrow functions in the first place, but about how I can solve, if at all, my problem, which consists of typing the function in JsDoc, because it seems to work in pure TyperScript:

function bar(init: "a"): () => string;
function bar(init: "b"): () => number;
function bar(init: any): any {
  return () => {
    switch (init) {
      case "a":
        return "abc";
      case "b":
        return 10;
      default:
        throw Error("Invalid body method.");
    }
  };
}

@MartinJohns
Copy link
Contributor

JSDoc currently does not support to document overloads in functions, see #25590.

@timonson
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants