2

In VSCode, if I write this JS file:

/** @type {number} */
let x = 10;
/** @type {string} */
let y = x;

nothing is underlined in red, and the tooltip over y simply gives let y: string. That is not what I want! I want VSCode to throw me an error, as it does in a TS file:

let x: number = 10;
let y: string = x;

In the file above, y is underlined in red and the tooltip when hovering over it gives TS2332: Type 'number' is not assignable to type 'string'

How can I achieve that? Should I tweak a VSCode setting? Should I use a particular ESLint or JSHint feature?

2 Answers 2

3

There is a VSCode setting for that:

  1. Open the settings (File > Preferences > Settings, shortcut Ctrl+,)
  2. Make sure the Javascript > Implicit project config > Check JS option has been checked
1
  • 1
    For me, it's now JS/TS > Implicit project config > Check JS
    – Thomas W.
    Sep 27, 2022 at 10:00
1

There are two different ways to handle that, the first one is the one @nino-filiu said

Open the settings (File > Preferences > Settings, shortcut Ctrl+,)

Make sure the Javascript > Implicit project config > Check JS option has been checked

So it will be enabled by default. This requires the rest of the team to do the same IDE config (in case you're not working alone) and it can be escaped at file level by adding // @ts-nocheck at the top of a specific file.

The second one is just adding // @ts-check at the top of your file so this specific file will be checked by default from now on and cannot be escaped unless either you change it to // @ts-nocheck or you delete this line.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .