javascripteslintjsdocecmascript-2020

validate object destructuring


Let's assume we have two functions

/**
 * @typedef MyResponse
 * @property {Number} id - my awesome id
 * @property {Array<Number>} attributes - list of ids
 * */

/**
 * @return {Promise<MyResponse>}
 * */
function request0() {
  return Promise.resolve({id: 1, attributes: [1,2,3]});
}

and

(async function () {
  const { foo } = await request0(); // but it doesn't return foo, it returns id and attributes
  console.log(foo);
})();

Is it possible to highlight somehow that kind of errors? I tried to find something on eslint but no luck.

Thanks


Solution

  • You could simply add // @ts-check in the begin of file without eslint.
    Then, you could see your foo have red line.
    But, you need to add it in every file with this way.
    More detail in Type checking JavaScript with vscode.

    enter image description here