0

Is it possible in Neovim to use JsDoc for typechecking of javascript files? Right now I use default LSP configurations for eslint and tsserver provided by lspconfig:

-- init.lua
require('lspconfig').tsserver.setup({})
require('lspconfig').eslint.setup({})

It works but seems to provide typechecks only for typescript files.

1 Answer 1

0

Just add a // @ts-check comment in the file. This will trigger type checking on normal JS files with TypeScript, as well as with JSDoc.

Example without // @ts-check:

with comment

Example with // @ts-check (notice line 4):

without comment

Here is an example with JSDoc being checked:

jsdoc example

4
  • Doesn't seem to work :( do you have any particular settings for tsserver LSP maybe? Or tsconfig.json/jsconfig.json files?
    – xaxa
    Commented Jun 15, 2023 at 11:10
  • Also, maybe LSP plugins are doing the trick for you here?
    – xaxa
    Commented Jun 15, 2023 at 11:11
  • Hmm, I use lsp-zero for my lsp setup and not lspconfig directly, maybe that's related? The file I showed you is not in a TS project, it was a blank directory. My LSP config was otherwise nothing special.
    – casraf
    Commented Jun 15, 2023 at 18:23
  • 1
    Oh, it seems to work but not completely. Look at this silly example: i.imgur.com/7ziwYoZ.png VSCode on top and vim on the bottom. return statement is highlighted as an error in VSCode because return type of the function as written in the JsDoc is {number} while in the code it can return null. Vim doesn't seem to capture that even if I add // @ts-check on the top line of the file.
    – xaxa
    Commented Jun 19, 2023 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.