DEV Community

Stop Wishing for TypeScript and Start Using JSDocScript in your Vanilla JS!

Bruno Noriller on March 25, 2023

First: no, didn’t create a new framework, so no worries. (JSDocScript isn’t really a thing, I think). Second: just use Typescript! But when you can...
Collapse
 
asmyshlyaev177 profile image
Alex

In my experience, JSDoc can't fully replace TS. For quickly check that there are no silly mistakes, you need tsc, and JSDoc types quickly become way more cumbersome than TS definitions.

But, big advantage of JSDoc is documenting code, surprisingly, can put usage example and link to documentation in it. It is great for reusable code, meanwhile TS good for static checks and IDE autocomplete.

Collapse
 
noriller profile image
Bruno Noriller

Verbosity aside, there are but a few cases where what you want is to pass generics, but then you have to make some casting that doesn't do what simply passing generics does.
Other than that, jsdoc gives the same static check and IDE autocomplete.

The thing about jsdoc for types is when you have some legacy project that wouldn't support TS otherwise. But for normal "everyday" apps, it's not something you would choose over just using TS.

For libraries, however, I can also see the appeal because it makes debugging and changing the lib far easier. Since you have the code that was written, it's easier to change and then upstream the changes.

Collapse
 
asmyshlyaev177 profile image
Alex

Types are most useful with complex cases, e.g. with generics, JSDoc maybe will never get all feature of TS, why MS would do that.

Thread Thread
 
noriller profile image
Bruno Noriller

But it does.

I've been using it for almost a couple of years and I can tell you that: as far as typing goes, you can do it with JSDoc. Check the article: dev.to/noriller/jsdoc-101-218c

It's not ergonomic to make some things in JSDoc, so I just make a d.ts file for those, but for most things... jsdoc will enable the same types and autocomplete as TS.

Thread Thread
 
asmyshlyaev177 profile image
Alex

So, struggle to implement generics and other things, no tsc for validation, and manually creating d.ts files.
It is a thing similar to running Doom on smart toaster, if you can do it, doesn't mean it is a good idea.

Thread Thread
 
noriller profile image
Bruno Noriller

But you can run tsc on .js files for validation.
And you can also manually create d.ts files in normal TS projects.
And its less "struggle" and more verbose.

Not to mention. Most cases and projects nowadays start out with TS from day zero. So it's not needed.
Also, most projects are not a lib that might gain from being easy to change the source code.

But, if you are in that few cases it benefits...

Thread Thread
 
asmyshlyaev177 profile image
Alex

You're missing the point.

Most of devs aren't so good and/or motivated, TS let's enforce types check, JSDoc doesn't. Even if you can perfectly live without TS, most of people who will work with your code are not so disciplined to spend more time on verbosity and don't make silly mistakes.
TS benefits teams, that why it get so widely adopted.

Thread Thread
 
noriller profile image
Bruno Noriller

Actually you can enforce it.

The point you're missing is that it's not for all projects. But for the ones where you can't use TS (or the even fewer cases where you know you get more from using jsdoc than TS.).

Thread Thread
 
asmyshlyaev177 profile image
Alex

I don't recall any way to enforce types check without TS, something that works only in VSCode won't cut it, need CI/CD as well.
Never seen project when you can't use TS.

Thread Thread
 
noriller profile image
Bruno Noriller

tsc works, you just need to add to check js in tsconfig.
it should be enough to show all the errors, but in vscode you might need to turn on a feat, see the other post where I say about it.

and there are legacy code with commonjs where you might be able to do whats needed to work with TS, but depending on the size of the projects it's easier to just use jsdoc instead of nothing at all.

Collapse
 
urielsouza29 profile image
Uriel dos Santos Souza

Svelte is leaving TypeScript

Collapse
 
noriller profile image
Bruno Noriller

I checked it out and I don't know why... they must have their reasons, but it's not something I would do. Typescript is more than just types.

This is certainly something to keep the eye on, thank you for bringing it up!

Collapse
 
emilysocool profile image
Emily Crookham

Hello Handsome

Collapse
 
voxpelli profile image
Pelle Wessman

Trying to gather some people doing this here as well: github.com/voxpelli/types-in-js/di...

Collapse
 
noriller profile image
Bruno Noriller

Nice!