Skip to content

Errors in curried function with overloads in JsDoc #49708

Closed
@timonson

Description

@timonson

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 () => {

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions