DEV Community

Discussion on: To TS or not to TS, that is NOT the question. Is it?

Collapse
 
patoi profile image
István Pató

There is no "JS vs TypeScript". The real question is: Do you NEED TypeScript? I don’t think you need to use it every project.

We are not using TS in a big client application (Svelte + JSDoc + d.ts, more than 400 files), and it's ok.

More: youtu.be/xLDVfBUgD8U
Do TypeScript without TypeScript - Simone Sanfratello / NearForm

Collapse
 
joelbonetr profile image
JoelBonetR 🥇

I'll check it out, thank you! :)

Collapse
 
jzombie profile image
jzombie

Are you manually writing that d.ts file instead? If so, it would seem it would be easier to just write in TS to begin with and let it generate the d.ts for you.

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

Good question here! While d.ts serves the purpose of documenting CommonJS modules (module.exports), JSDoc aims to document JS code and can be used to provide type information in JavaScript files which, along with TS pragma (// @ts-check) will make VSCode to use TS to automatically search for type notations and type checks whenever you document a function, variable etc. With that notation, Including ES6 modules export.

Leaving that alone, JSDoc + TS Pragma is a safely approach (first step) to migrate a JS project into TS or, if you don't want to add TS as dependency into your project or your team has zero or near-zero experience with TS, you can use this to provide type notations plus type checks without actually coding in TS, just by documenting your code which is always good.

Thread Thread
 
jzombie profile image
jzombie

I have no disagreements with using JSDoc. I find it incredibly useful.

Manually managing a d.ts, however, I'm not sure about.

There's the potential of getting those types completely incorrect if manually managing that file separately.

I'd think that if the aim is to use d.ts without TypeScript, surely someone has written some sort of parser which could generate them from JSDoc itself.

At that point, I'd just recommend using TypeScript, however.

Thread Thread
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

No no, that's nothing related with d.ts. It's just that writing JSDoc this way (check TS doc around JS Doc I linked before) and adding // @ts-check at the top of your JS files, VSCode will type check your variables, functions/methods and so on using TS behind the scenes.

Try it out quickly on a single JS file if you're curious so it will be more clear probably 😁