Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda