Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
Custom `@typedef` object or `undefined`

I am trying to properly document a scenario that happens throughout out a program I am working in. I am unsure how to properly express the expected resolution of certain promises. Below is my attempt to mock what is happening.

There are certain api promises, outside the scope of the program that I am trying to document. I mentioned this because documenting the api methods is not an option, but rather the variable that contain the resolved values of those promises is where I'd like focus.

// simplified example
function apiMethod() {
    const fail = 0;
    const response = {
        error: fail ? "error message" : undefined, // undef if no errors
        success: fail ? undefined : [ // undef if errors present
            {key01: "val01", key02: 1, key03: {"subKey01": [1, 2, 3]}},
            {key01: "val03", key02: 2, key03: {"subKey01": [4, 5, 6]}}
        ]
    };
    return new Promise(resolve => setTimeout(() => resolve(response), 250));
}
(async () => {
    /**
     * @typedef {object} thing
     * @property {string} key01
     * @property {number} key02
     * @property {{subKey02: string[]}} key03
     */
    /**
     * @type {Promise<{success: thing[]|undefined, error: string|undefined}>}
     */
    const myVar = await apiMethod();
    console.log(myVar.key01);
})();

This seems to work mostly well, but where I am getting stuck on is expressing that the success property could be undefined under certain circumstances. Right now this is how the vscode tooltip looks when hovering the myVar;

const myVar: Promise<{
    success: thing[];
    error: string | undefined;
}>

Note the missing undefined for success

This is how the vscode tooltip looks when hovering thing in the @type expression:

type thing = {
    key01: string;
    key02: number;
    key03: {
        subKey02: string[];
    };
}

Questions:

  1. Is is possible to express that success will be an array of thing objects or undefined? When using typedef it doesn't seem like I can do this.

  2. Right now it only seems possible to see the exploded view of the thing object when hovering thing in the @type expression. When I hover myVar it simply shows that success will be an array of things. Is it possible show this "exploded" view of thing when hovering myVar, whilst still using @typedef?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda