3 ms·
You can get a lot of the benefits of Typescript in VSCode without leaving JavaScript behind by using JSDoc. Typescript will pick up the JSDoc type annotations
by scottfr 6y ago
You can get a lot of the benefits of Typescript in VSCode without leaving JavaScript behind by using JSDoc.
Typescript will pick up the JSDoc type annotations and use them to type the code. This can be a great option when typing an existing project.
Docs: https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html#supported-jsdoc https://www.typescriptlang.org/docs/handbook/type-checking-j...
- hombre_fatal 6y agoVanilla VSCode also has surprisingly good code inference for Javascript/Typescript. Recently, Typescript was complaining about an implicit `any` even though the VSCode tooltip clearly could infer the actual type (it confusingly was showing both cases in the tooltip). I finally realized that VSCode's built-in inference worked here but Typescript didn't work at all because I forgot I was importing a .js file rather than a .ts file. VSCode for course also downloads @types/* files in the background even in vanilla JS projects which is super helpful because you get effortless intellisense. That it does this kind of stuff out of the box is the sort of reason why I've lost my Vim/Emacs fanaticism over the years.
- DanRosenwasser 6y agoVS Code's JS support is actually still powered by TypeScript. I'm not 100% sure exactly what you were experiencing, but it's intentional that VS and VS Code automatically download types when powering JavaScript contexts because we try to make things "just work" for JS users.
- hombre_fatal 6y agoI don't think you are saying anything in conflict with me. VSCode tries to give you type info in contexts where Typescript wouldn't (e.g. importing Javascript files, in a completely non-TS project) so it makes sense that its inference tooltip can be more helpful. You can easily recreate this by importing a fn from a JS module and then hovering it in VSCode (also configured for Typescript): VSCode knows the fn signature but Typescript won't. Both cases show up in the tooltip (though confusingly). At least, that's my interpretation of this sort of tooltip: https://i.imgur.com/lXS5teK.png https://i.imgur.com/lXS5teK.png And to be clear, your last sentence was my exact praise for VSCode at the end of my own comment.
- tomnipotent 6y ago> intentional that VS and VS Code automatically download types Are these coming from DefinitelyTyped?
- WorldMaker 6y agoIndirectly, sometimes. They come from npm, but the largest chunk of type packages for libraries that do not provide their own types on npm are serviced from DefinitelyTyped (via the `@types` organization and its long tail of shadow packages).
- emilecantin 6y agoYou can get a lot of this nice behaviours in Vim with coc.nvim (if you can get past the awful name). It uses VSCode's language server behind the scenes to offer similar auto-complete / tooltips. It doesn't auto-download @types, but it does suggest them to you. It can also auto-import, rename across files, etc.
- pierreyoda 6y agoUnfortunately, I've found at my previous employer that JSDoc is incredibly limited and fails at some basic structures, has very limited generic support and will not understand "advanced" structures like type mappings. Re-using types (structs or union types) is also a huge pain.