-1

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

Alexey Sh.
  • 1,734
  • 2
  • 22
  • 34
  • I don't see a problem here, you destruct the value from request0 which is a resolved promise containing those props – asafel Dec 16 '20 at 09:03
  • 1
    That depends on the level of JSDoc support of your editor. A static type checker like TypeScript would also catch it. – str Dec 16 '20 at 09:05
  • @asafel the request0 returns id and attribute and it is documented through jsdoc notes. it doesn't return foo property – Alexey Sh. Dec 16 '20 at 09:07
  • @str yes, kind of typescript but with jsdoc. it can work with accessing to a property of a plain object, but it doesn't work in destructuring case – Alexey Sh. Dec 16 '20 at 09:09
  • https://marketplace.visualstudio.com/items?itemName=tusaeff.vscode-typescript-destructure-plugin - There is a plugin for VSCode, which will highlight. – Shankar Ganesh Jayaraman Dec 16 '20 at 09:20

1 Answers1

1

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

Jack Yu
  • 2,190
  • 1
  • 8
  • 14
  • 1
    intelij idea also supports that kind of checking https://blog.jetbrains.com/webstorm/2019/09/using-typescript-to-check-your-javascript-code/ – Alexey Sh. Dec 16 '20 at 13:44