Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
In Javascript file, How to comment overloading function signature in tsdoc/jsdoc(can be recognized by vscode intellisense)

I have these definitions:

class Entity {}
class Point {}
class Edge {}
class A {
  from(x, y) {
    if (x == null) {
      return this.remember('_from')
    } else if (x instanceof Entity || x instanceof Point) {
      this.remember('_from', new Edge(x))
      return this
    } else {
      this.remember('_from', new Edge(new Point(x, y)))
      return this
    }
  }
}

The from method has multiple signature as shown above. I want to write the jsdoc comment on it for vscode intellisense using.

I have two ways of it, but neither of them is worked:


class A {
  /**
   * @type {((x: number, y: number) => this) & ((x: Entity|Point) => this) & (() => Edge)}
   */
  from(x, y) {
    if (x == null) {
      return this.remember('_from')
    } else if (x instanceof Entity || x instanceof Point) {
      this.remember('_from', new Edge(x))
      return this
    } else {
      this.remember('_from', new Edge(new Point(x, y)))
      return this
    }
  }
}


class A {
  /**
   * @template T
   * @typedef {{
   *  (this: T, x: number, y: number): T;
   *  (this: T, entity: Entity): T;
   *  (this: T, point: Point): T;
   *  (): Edge;
   * }} fromType
   *
   * @type {fromType<this>}
   */
  from(x, y) {
    if (x == null) {
      return this.remember('_from')
    } else if (x instanceof Entity || x instanceof Point) {
      this.remember('_from', new Edge(x))
      return this
    } else {
      this.remember('_from', new Edge(new Point(x, y)))
      return this
    }
  }
}

I have now a workround, but it's not an overloading, since the parameter x can not be prompted with exact type.


class A {
  /**
   * @template {unknown} T
   * @param {T} [x]
   * @param {number} [y]
   * @returns {T extends Entity|Point|number ? this : Edge}
   */
  from(x, y) {
    if (x == null) {
      return this.remember('_from')
    } else if (x instanceof Entity || x instanceof Point) {
      this.remember('_from', new Edge(x))
      return this
    } else {
      this.remember('_from', new Edge(new Point(x, y)))
      return this
    }
  }
}

So is there a way to write correct overloading of function signature in tsdoc and how to write it if yes? Thank you.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!