A few things I've come to personally believe after spending years developing web and robotics software in Python/JavaScript then spending years having to maintain while constantly adding new features and dealing with company pivots:<p>- The types exist whether you write them down or not.<p>- If they're not written down, they're written down in your head.<p>- Your head is very volatile and hard for others to access.<p>- Typing is an incredibly good form of documentation.<p>- JSDoc and TypeScript are standards/formats for typing. Like any tools, they both have advantages and disadvantages. Neither is objectively better than the other.<p>- Make informed decisions on how you'll describe your types, and then be consistent and unsurprising.<p>- A type checker is the computer saying, "okay then, prove it" about your program's type validity.<p>- Not every program benefits from the same amount of "prove it."<p>- Too much can be as bad as too little. You're wasting resources proving throwaway code.<p>- I like languages that let you decide how much you need to "prove it."
> Your head is very volatile and hard for others to access.<p>One of the lessons you learn while doing this job is that "others" includes "yourself in the future".<p>(Of course people will tell you this way before you find out yourself, but what do they know...)
> - I like languages that let you decide how much you need to "prove it."<p>Rust is known for being very "prove it," as you put it, but I think that it is not, and it exposes a weakness in your perspective here. In particular, Rust lets you be lax about types (Any) or other proved constraints (borrow checker bypass by unsafe, Arc, or cloning), but it forces you to decide how the unproven constraints are handled (ranging from undefined behavior to doing what you probably want with performance trade-offs). A langauge that simply lets you not prove it still <i>must</i> choose one of these approaches to run, but you will be less aware of what is chosen and unable to pick the right one for your use case. Writing something with, for example, Arc, .clone(), or Any is almost as easy as writing it in something like Python at the start (just arbitrarily pick one approach and go with it), but you get the aforementioned advantages and it scales better (the reader can instantly see (instead of dredging through the code to try to figure it out) "oh, this could be any type" or "oh, this is taken by ownership, so no spooky action at a distance is likely").
> JSDoc and TypeScript are standards/formats for typing. Like any tools, they both have advantages and disadvantages. Neither is objectively better than the other.<p>Agreed. Just to clarify, my intentions with this post weren't to advocate for one over the other. Just to point out that they are the same thing. They are both TypeScript.
And if your boss is the type to pivot way too often and want it done yesterday, then they don’t deserve code that is prove in the right places so you just get an LLM to add typing to everything after the fact…
> I like languages that let you decide how much you need to "prove it."<p>I love that too. Are there any other languages than TS that have this as a core design feature?
I'll take a more definite position. Build pipelines are the devil. Every square inch of surface complexity you add to whatever you're doing, you will pay for it. If there's a way to get the benefits without the build step, that's your choice. You may need runtime type enforcement, so that's not JSDoc, and you're doing that with... JS so that's already suspicious but the use case def exists. JS, the DOM, HTTP, this whole stack is garbage at this point so arguing about the type system is a little idk hard to care about personally. I have a solution that lets you discard most of the web stack, that's more interesting to me.
I'm a fan of anything that allows me to build with javascript that doesn't require a build step.<p>Modern HTML/CSS with Web Components and JSDoc is underrated. Not for everyone but should be more in the running for a modern frontend stack than it is.
On the one hand I can see the appeal of not having a build step. On the other, given how many different parts of the web dev pipeline require one, it seems very tricky to get all of your dependencies to be build-step-free. And with things like HMR the cost of a build step is much ameliorated.
I have not written a line of JavaScript that got shipped as-is in probably a decade. It always goes through Vite or Webpack. So the benefit of JS without a build step is of no benefit to me.
Webcomponents are a pain in the ass to make, though. That is, sufficiently complex ones. I wish there was an easier way.
It's ok now, at least for me. There are still challenges around theming and styling because of styling boundaries (which makes Web Components powerful, but still). A part of it is about tooling, which can be easier to improve.<p>Try my tiny web components lib if you want to keep JSX but not the rest of React: <a href="https://github.com/webjsx/magic-loop" rel="nofollow">https://github.com/webjsx/magic-loop</a>
I've built Solarite, a library that's made vanilla web components a lot more productive IMHO. It allows minimal DOM updates when the data changes. And other nice features like nested styles and passing constructor arguments to sub-components via attributes.<p><a href="https://github.com/Vorticode/solarite" rel="nofollow">https://github.com/Vorticode/solarite</a>
They could have better ergonomics and I hope a successor that does comes out but they're really not that bad.
web components need 2 things to be great without external libraries (like lit-html):<p>- signals, which is currently Stage 1 <a href="https://github.com/tc39/proposal-signals" rel="nofollow">https://github.com/tc39/proposal-signals</a><p>- And this proposal: <a href="https://github.com/WICG/webcomponents/issues/1069" rel="nofollow">https://github.com/WICG/webcomponents/issues/1069</a>
which is basically lit-html in the browser
Agreed on native HTML+CSS+JSDoc. An advantage in my use-cases is that built-in browser dev tools become fluid to use. View a network request, click to the initiator directly in your source code, add breakpoints and step without getting thrown into library internals, edit code and data in memory to verify assumptions & fixes, etc.<p>Especially helpful as applications become larger and a debugger becomes necessary to efficiently track down and fix problems.
TS is worth the build step.
Why? The half a second for the HMR is taking up too much your day?
No, because layers of abstraction come at a cost and we have created a temple to the clouds piled with abstractions. Any option to simplify processes and remove abstractions should be taken or at least strongly considered.<p>Code written for a web browser 30 years ago will still run in a web browser today. But what guarantee does a build step have that the toolchain will still even exist 30 years from now?<p>And because modern HTML/CSS is powerful and improving at a rapid clip. I don't want to be stuck on non-standard frameworks when the rest of the world moves on to better and better standards.
So, some history. When SPA's started to boom on the web JSDoc was a life saver for typing. Application state was getting more complex. We needed more guard rails.<p>Then Google Closure Compiler came along which added type safety via JSDOC and TS came along with (TS)JSDoc support and it's own TS syntax.<p>The community chose native TS and Google Closure compiler slipped away into the background.<p>So (TS)JSDoc support is a relic from when Microsoft was trying to get market share from Google.<p>Today in 2025, TS offers so much more than the (TS)JSDoc implementation. Generics, Enums, Utility types, Type Testing in Vitest, typeguards, plus other stuff.<p>Today I use TS. I also use plain JSDoc for documentation. e.g. @link and @see for docs. Or @deprecated when I'm flagging a method to be removed. @example for a quick look up of how to use a component.<p>TS and plain JSDoc are both important together. But (TS)JSDoc alone, is a relic of the past.
> Today in 2025, TS offers so much more than the (TS)JSDoc implementation. Generics, Enums, Utility types, Type Testing in Vitest, typeguards, plus other stuff.<p>This was my main impetus for writing this article. Modern JSDoc uses the TypeScript language service. You can use generics, utility types, typeguards (including the `is` keyword), regex parsing, etc all with just JSDoc.<p>I used these features extensively (especially generics) in a personal project and managed to do it all in JSDoc.
JSDoc is missing a lot of basic capabilities. For example a TypeDef is automatically exported which can cause collisions and forces you to repeat or inline types.<p>Types for classes are poor and often you'll find yourself creating a `.d.ts` file or `.ts` file to export non trivial types - however the target file doesn't know how to consume them.
typedefs are indeed automatically exported but that doesn't mean collisions can happen. You would still have to explicitly import a type<p>Regardless, I hardly consider that a "missing basic capability"<p>I don't know what you mean about types for classes being "poor". Types for classes work exactly the same way
You cannot replicate `import type { x } from './foo'` without also re-exporting that import - which causes collisions.<p>The alternative is to do an inline `const foo = /** @type {import('./foo').x} */ ({})` however this gets messy, repetitive and it's difficult to use algebraic types (e.g. `Event & { detail: string }`)
JSDoc does not understand typescript syntax though? The typescript language server just kinda plows through/over JSDoc sure, but try getting JSDoc to parse some of the TS-ified things that JSDoc has alternatives for.<p><a href="https://github.com/jsdoc/jsdoc/issues/1917" rel="nofollow">https://github.com/jsdoc/jsdoc/issues/1917</a><p><a href="https://github.com/jsdoc/jsdoc/issues/1917#issuecomment-1250760267" rel="nofollow">https://github.com/jsdoc/jsdoc/issues/1917#issuecomment-1250...</a>
tuples, `&` operator, and even generics all work perfectly well inside a `@type` declaration. For example:<p>```js<p><pre><code> /**
* @type {{
* slug: `${string}_${number}`;
* id: number;
* } & { status?: [code: number, text: string]; }}
*/
const example = { slug: 'abc_34', id: 34 };
</code></pre>
is the exact equivalent of<p>```ts<p><pre><code> const example: {
slug: `${string}_${number}`;
id: number;
} & { status?: [code: number, text: string] } = { slug: 'abc_34', id: 34 };
</code></pre>
For TS-specific keywords like `satisfies`, there's a corresponding JSDoc keyword like @satisfies. Generics use @template.<p>Is there any specific feature you think is not supported? I'm sure I could work up a TS Playground example.
> Is there any specific feature you think is not supported<p>Yeah, uhm, most of what you've been posting? :). That JSDoc example above gives:<p><pre><code> ERROR: Unable to parse a tag's type expression for source file /Work/lol-jsdoc-why/index.js in line 1 with tag title "
type" and text "{{ slug: `${string}_${number}`; id: number;} & { status?: [code: number, text: string]; }}": Invalid type expre
ssion "{ slug: `${string}_${number}`; id: number;} & { status?: [code: number, text: string]; }": Expected "!", "$", "'", "(",
"*", ".", "...", "0", "?", "@", "Function", "\"", "\\", "_", "break", "case", "catch", "class", "const", "continue", "debugger",
"default", "delete", "do", "else", "enum", "export", "extends", "false", "finally", "for", "function", "if", "implements", "impor
t", "in", "instanceof", "interface", "let", "new", "null", "package", "private", "protected", "public", "return", "static", "supe
r", "switch", "this", "throw", "true", "try", "typeof", "undefined", "var", "void", "while", "with", "yield", "{", Unicode letter
number, Unicode lowercase letter, Unicode modifier letter, Unicode other letter, Unicode titlecase letter, Unicode uppercase let
ter, or [1-9] but "`" found.
</code></pre>
Edit: Also, your first edit says Webpack switched from TypeScript to JavaScript, but Webpack source was never written in TypeScript.
We're talking about two different things here. All my examples work perfectly fine with TypeScript<p><a href="https://www.typescriptlang.org/play/?#code/PQKhCgAIUgBAXAngBwKaQN4ajSkDOANgK4DmAXJAAYAkG+8ATgJYB2pAvgPp2vEC2AI1SMOVANw48kZgBNKfISMnRIHSADJMBeAEN4xfAH5KAbQDGAe1moFA4YwA0keKgAe8SgxbsAuuLUOHGBwK1YGSHddfmRCdABebSIySgByXUFzLgBmABZU5zlKPLVJIA" rel="nofollow">https://www.typescriptlang.org/play/?#code/PQKhCgAIUgBAXAngB...</a><p>You are attempting to generate documentation from jsdoc comments using an npm package that is also called "jsdoc". Ofc in this case "JSDoc <i>is not</i> TypeScript". That package only supports the subset of JSDoc that is relevant to it. Though I believe you can use TypeDoc instead if you want to generate documentation from JSDoc that contains typescript types.<p>In the post I made it explicit that I'm talking about intellisense, developer tooling, type checking etc. You can run `tsc` to do typechecking on a project typed with JSDoc like the examples I've given throughout this thread just fine.<p>I guess the difference here is I'm coming at this from the perspective of "what is TypeScript used for. Can JSDoc comments substitute that". And the answer is almost completely yes.<p>Also tbh I've never met anyone that uses that package to generate API docs. I don't think it's a very modern package: <a href="https://github.com/jsdoc/jsdoc/issues/2129" rel="nofollow">https://github.com/jsdoc/jsdoc/issues/2129</a>
Apologies, my first draft of that comment got deleted on a refresh (mobile) and my posted one left out how I'm probably being too pedantic: the official JSDoc is not TypeScript.<p>Your post is actually one of the more accurate ones compared to others that say "you don't need typescript" with the big caveat that you actually need a whole lot of the typescript ecosystem to make JSDoc work.<p>I just wish there was an official handover, or a more clear delineation between JSDoc and Typescript JSDoc Extensions.
I think you have a valuable point. I kinda purposely avoided explicitly defining what JSDoc is. Instead I'm relying on "the JSDoc we're all familiar with". I said in the post that if your IDE is giving you intellisense from JSDoc comments then you are almost certainly already using TypeScript. That's about as close as I got to defining the JSDoc I'm talking about<p>But given that JSDoc doesn't have any sort of formal spec, I think the distinction you're making is more of a historical than a technical one.
I’m curious how type checking is possible in a JDoc project. As far as I’m aware there’s no way to get type checking to work without tsc or a TypeScript LSP plugin.
That is exactly how it works. Any IDE providing you intellisense is using the TypeScript language service. That's why this article is called "JSDoc is TypeScript". If you see a red squiggly because of your JSDoc comment, you are almost certainly already using TypeScript (without any .ts files)
The article is an explicit response to the point you’re making.
> Take it from a massive TypeScript nerd: JSDoc is not an anti-TypeScript position to take. It is the same powerful static analysis without the build step.<p>Ahum <a href="https://nodejs.org/en/learn/typescript/run-natively" rel="nofollow">https://nodejs.org/en/learn/typescript/run-natively</a>
Technically there's still a build step happening under the hood! But another benefit of shifting TS into JSDoc comments is that it also works in browsers. That being said, while I understand the aversion to unnecessary complexity, I'm personally fine with a build step to remove TS. It's a much nicer syntax IMO. And with tools like `swc` for type stripping, it's pretty snappy too.
that's limited to node. won't work in a browser.
1. there are plenty things you can't express in jsdoc but can in typescript, flow did the right thing here where you have access to full language, not sure why typescript never did it, they could, with the same syntax flow is using<p>2. you can have navigation that goes to typescript file instead of definition, just arrange your exports in package.json correctly (first ones take precedence)
Well I'd love to hear some concrete examples if you have any on hand! I was of the same opinion as you until I refactored a project of mine to use JSDoc.<p>Since any TypeScript type can be expressed in JSDoc, I imagine you're mostly thinking of generics. At least that was my main sticking point. JSDoc does actually have generic slots with the @template tag. Actually using them in practice is a little unintuitive but involves typing the return type. E.g. for a function it'd look like this:<p><pre><code> /** @type {ReturnType<typeof useState<Book[]>>} */
const [books, setBooks] = useState();</code></pre>
Last time I checked, there isn't a way to extend types (<a href="https://www.typescriptlang.org/docs/handbook/2/objects.html#extending-types" rel="nofollow">https://www.typescriptlang.org/docs/handbook/2/objects.html#...</a>) that works exactly like in TypeScript.
JSDoc actually has the @extends tag<p><pre><code> /**
* @typedef {object} Dog @extends Animal
* @property {string} childProp
*/
</code></pre>
But I don't really use that feature in TypeScript. Instead I rely on `&`. This works in exactly the same way in JSDoc.<p>Also if you're curious about the equivalent of `extends` in generic slots, here's an example I have from a different project<p><pre><code> /**
* @template {Record<string, unknown>} [T=Record<string, unknown>]
* @typedef {{
* children?: NewickNode<T>[];
* name?: string;
* length?: number;
* data?: T;
* }} NewickNode
*/
</code></pre>
The generic slot here, T, is "extended" by Record<string, unknown>. The equivalent in TypeScript would look like<p><pre><code> type NewickNode<T extends Record<string, unknown> = Record<string, unknown>> = {
children?: NewickNode<T>[];
name?: string;
length?: number;
data?: T;
};</code></pre>
the equivalent in typescript would be "export type" not just "type", since as I pointed out that type is exported without you being able to control it
I don't understand why someone would opt for "write Typescript, but add a bunch of extra characters, make the syntax clunkier, and throw away all the benefits of compile-time errors because we'd rather have runtime errors" in order to save, what, a microsecond stripping typescript out of TS files?<p>Everyone's complaining about "the build step" but the build step is just an eye blink of stripping out some things that match a regex.
> throw away all the benefits of compile-time errors because we'd rather have runtime errors<p>This is inaccurate on multiple counts. First of all, you can still run tsc with JSDoc if you want a hard error and you can still use strict mode with JSDoc. Your tsconfig file governs JSDoc-typed code just the same as it governs .ts-typed code. In both cases you can also ignore the red squigglies (the <i>exact same</i> red squigglies) and end up with runtime errors.<p>Nobody is advocating for reduced type safety or increased runtime errors.<p>I also think there are many valid reasons to loathe a build step (like not dealing with the headache that is the discrepency between the way the TS compiler deals with import paths vs js runtimes).<p>All that being said, I'm not really trying to convince anyone to stop using TypeScript. I'm simply pointing out that using JSDoc <i>is</i> using TypeScript. It's the same language service.
You can use `&` operator to combine types. Works for adding fields, making branded types, etc.
Support for generics is limited in JSDoc compared to TypeScript, especially when arrow function is involved. Things that work fine in TypeScript trigger errors even though they are syntactically the same.
Because AirBnB's ESLint config has been burned into my brain I almost only use arrow functions. I also use generics extremely often. It's definitely a little more clunky but I haven't run into anything you can do in TypeScript that you can't do in JSDoc.<p>JSDoc also allows you to type stuff in-line. For example I often have to type an empty array like so:<p><pre><code> const [books, setBooks] = useState(/** @type {Book[]} */([]));
</code></pre>
If you have a tangible example of a problem you've run into, I'd love to walk through it.
JavaScript example:<p><a href="https://www.typescriptlang.org/play/?filetype=js#code/PTAEAEBcGcFoGMAWBTeBrAUB4AqHHQcJJkBbABwBsBDE0AFQKPHmssoCNr1QB1AS0iIAMgHsA5uP4A7cQGE2nbpkIRy1AE7VSoAN4AzAK7T4kfqOkAKAJQAuegF9Qrdl3RMIG5JEMbp0PUcmYCxcfFUoMipaZAYPKABPcljdASExSRl5RTc0AB56AD4HYIx4C2hIUAB3QREJKVlQAF5QfRbCvQJQHvL-UUpkADpKCUsAclHM2QBCcetu3oqqr2hDSirW-RsAbkXQLx8-A+Q1jb2HPYwjEzMLUHFvAC1kDVFQGy6ensPfaVAAAwXUJ4eLqLQ6VJ1DKNbKuZROADaU1hzVq6QaWQAuvFfn4ArppIZSBxXiVCCEbqZzP8ACaiXiiDRoD4orItGrQzGyaxfb54-5s2SWR6QF5vawXIA" rel="nofollow">https://www.typescriptlang.org/play/?filetype=js#code/PTAEAE...</a><p>Almost equivalent typescript code:<p><a href="https://www.typescriptlang.org/play/?#code/C4TwDgpgBA6glsAFgGQPYHN1wHboMICGANkQEYEDGA1lALxQA8AKgHwAUFxZlVAXFGwCUdFlCbDaopgG4AULIqpsAZ2BQA7ghQYsufvCRpMOfF3LU6UAGYioAb1lQnURStREIAOiIY2Ach9jXABCP0FHZ1dVKAAnCGUAVyI1eishOWdYiGAEmOwsxOS5AF95KwTsCmA4JSh0bIAtCBjUAWEHTLicvKgABhKyiqqa-IATVBhUGJo2QN10fS0jecIScxp6TUMdE3aIpy7c-LmTNnrgJpbBEqA" rel="nofollow">https://www.typescriptlang.org/play/?#code/C4TwDgpgBA6glsAFg...</a><p>(I had to make it a little bit different from the JS code to make it compile)<p>(Well, this is not exactly about arrow function I guess. I remembered that part wrong.)<p>Note that I cannot make the type check in JS code to pass. Whatever I do, there is always a error. Meanwhile, it does not take much to TS code to work.
and types can be written in .d.ts file(s), which can be used in jsdoc out of the box (no import needed)
ReturnType is TypeScript, no? You’re using JSDoc to express but it’s a TypeScript type.
As I stated in the article, JSDoc <i>is</i> TypeScript :P<p>TypeScript utility types are available in JSDoc. You can pretty much copy-paste any typescript Type/Interface into JSDoc
Isn't that the whole point of the article? For all intents and purposes, JSDoc IS TypeScript
> there are plenty things you can't express in jsdoc but can in typescript<p>This isn't really true anymore, they have systematically added pretty much every type system feature to the JSDoc-like syntax.
TypeScript's type system is Turing complete so you have access to essentially unlimited expressivity (up to the typechecking termination depth): <a href="https://news.ycombinator.com/item?id=14905043">https://news.ycombinator.com/item?id=14905043</a>
> For packages typed with JSDoc, CTRL/CMD clicking on a function will take you to actual code rather than a type declarations file. I much prefer this experience as a dev.<p>ok i didn't think about this, that's an underrated benefit
5 years ago I was at a meet up and the guy talking was saying how if you don't like typescript these jsdocs are the way to go. Had to explain to my employer attending that it is still typescript. Didn't seem to believe me and was super against typescript but not jsdocs lol
The difference is syntax compression imo.
> Had to explain to my employer attending that it is still typescript<p>"is" is doing a lot of heavy lifting there: JSDoc and TypeScript are two different ways to explicit prescribe typing in a way that tooling can use to determine correctness. The TS syntax is _far_ more powerful, but JSDoc can do most of the common TS use cases, for folks who want to stay in JS land while still benefiting from type tooling (either invoked or straight up built into the IDE).
> in a way that tooling can use to determine correctness.<p>As I pointed out in the article, the "tooling" is exactly TypeScript language services. If you are using JSDoc and you get squigglies or intellisense or any other similar features, you <i>are</i> using TypeScript.<p>You can copy-paste basically any bit of TypeScript into a JSDoc comment and it will work. JSDoc supports any non-runtime feature of TypeScript (so not enums). Even generics! You can even reference TypeScript utility types!<p>The whole point of this article was to correct the idea that JSDoc is not TypeScript. It absolutely is! There's almost nothing you can't define in JSDoc that you can't define in a .ts file. Albeit with a sometimes clunkier syntax
> If you are using JSDoc and you get squigglies or intellisense or any other similar features, you are using TypeScript.<p>This is true in the same way you are "using" C++ if you are on Windows. When most people say "use XYZ language" they mean "are personally writing code in XYZ language" rather than "under the hood my code is transpiled to this other language I don't write in"
In practice today JSDoc 100% completely and utterly always is Typescript.<p>It might not have been so originally. It might still be possible to do differently. But in practice today you are getting Typescript in your JSDoc with the out of the box tooling that is everywhere.
Not when the TS team refuses to fix long standing issues preventing JSDOC from operating properly in VSC, for example <a href="https://github.com/microsoft/TypeScript/issues/16665" rel="nofollow">https://github.com/microsoft/TypeScript/issues/16665</a>
To be honest, I find Ryan Cavanaugh's argument against this quite convincing. It's weird to have something documented if you import the .ts file, but not if you import a .d.ts generated from it. If you want to show the value of the default argument of a function, you should probably just add it to the doc comment — not the type.
counterpoint: JSDoc <i>is not</i> typescript<p>If you define a type in a file with @typedef, it is automatically exported and there is nothing you can do to control that: <a href="https://github.com/microsoft/TypeScript/issues/46011" rel="nofollow">https://github.com/microsoft/TypeScript/issues/46011</a><p>I tried making a library this way and lacking control over the visibility of the exported types was really painful; it made my intellisense awful because every type I defined at the root was exported from the library
I really like it for web components. Lately I have many "my-component.js" files and it's quite nice to just be able to copy them to new projects and have it all work without a build step. But I'm not sure I would use JSDoc over typescript syntax in a large project.
That's technically a fair complaint but feels like a minor nitpick. Just don't `@import` the types you don't want to use.
Been there, tried that. When starting to write JavaScript in anger 3 or 4 years ago, I started out with JSDoc, because my impression was also, hey, that is like TypeScript, but closer to the metal and maybe with less magic. A few months in I realised that TypeScript is just a better and more well-rounded implementation of what JSDoc has to offer (I don't remember now, but there were annoying things that you would expect would work in JSDoc, but didn't quite).<p>Just use TypeScript.
TypeScript won over the alternatives, exactly because it is only a type checker, and not a new language.<p>Granted they initially weren't down that path, but they course corrected it on time, and not much people use stuff like enums in new code.
I'm actually using JSTypes in app, I don't mind it.<p>I choose to use it because I didn't want to deal with a build step for a smaller project. The project has grown and I am looking at adding a build step for bundling but still not too worried about using JSDoc over TS.<p>This might be my config, but one thing that does annoy me is whenever I define a lambda, I need to add an doc type. I guess if that's disincentivising me from writing lambdas maybe I should just add a TS compile step lol.<p>----------------------<p>Here's an example
- I got some config typed with this function <a href="https://github.com/AKST/analysis-notebook/blob/c9fea8b465317f41da2a10d7c1d493c811803e32/lib/app/sec-unsw/sec-2102/sec-05-3/util.js#L11-L14" rel="nofollow">https://github.com/AKST/analysis-notebook/blob/c9fea8b465317...</a>
- Here's the type <a href="https://github.com/AKST/analysis-notebook/blob/c9fea8b465317f41da2a10d7c1d493c811803e32/lib/app/sec-unsw/sec-2102/sec-05-3/type.ts#L18-L36" rel="nofollow">https://github.com/AKST/analysis-notebook/blob/c9fea8b465317...</a>
- And here's something to generate a more complicated type for defining config knobs <a href="https://github.com/AKST/analysis-notebook/blob/c9fea8b465317f41da2a10d7c1d493c811803e32/lib/base/runtime/config/type.ts#L52-L60" rel="nofollow">https://github.com/AKST/analysis-notebook/blob/c9fea8b465317...</a>
I work in a codebase that unfortunately does not support TypeScript. I use JSDoc extensively, although not with type check enabled (due to various limitations). I also work on other projects with TypeScript. My own experience is that the DX with "real" TypeScript is much, much better than JavaScript with JSDoc, without question. JavaScript with JSDoc is much more verbose, with a lot of limitations when types get long or complex compared to TypeScript. The official TypeScript language service also does not provide the same level of support in very subtle ways.<p>Basically, the fact that it works does not mean it works well, and I don't recommend anyone going in this other direction unless they understand what they are getting into.
> although not with type check enabled (due to various limitations)<p>Curious what limitations there are on static type checking. It seems like a matter of your IDE setup<p>> My own experience is that the DX with "real" TypeScript is much, much better than JavaScript with JSDoc<p>I agree with the DX point. I would just point out that if you're using JSDoc and getting intellisense from it, you <i>are</i> using TypeScript
Not a limitation in language service, but an issue with the codebase itself -- legacy codebase with a lot of missing/incorrect typing from upstream JavaScript code (owned by other teams), making it practically impossible to actually run type check.
> For packages typed with JSDoc, CTRL/CMD clicking on a function will take you to actual code rather than a type declarations file. I much prefer this experience as a dev.<p>More broadly, this is the default behavior of JS even without JSDoc blocks, and it ought to be the default behavior <i>everywhere</i>, including TS. I'm not alone in this sentiment, but it's incredibly contentious. There's been an open GH issue about it with hundreds of replies for years. I have no idea why they can't just pick a different shortcut for viewing types and call it a day. They'd be doing the entire ecosystem a favor.
JSDoc works great for buildless application setups! One downside is that if you publish a library to npm you still need a build step to generate .d.ts files from your JSDoc type annotations so that npm shows a "TS" badge on the npm package page. This also seems to apply to VSCode's intellisense which keeps trying to poke you to "try to install @types/jsdoc-typed-package to get type information". Other JS ecosystem tooling also doesn't seem to process JSDoc types at all such as jsdocs.io or tsdocs.dev. So for libraries we're stuck with .d.ts generation via "tsc --allowJs --checkJs --declaration ..." even if it's all JS.<p>npm displays packages with bundled TypeScript declarations <a href="https://github.blog/changelog/2020-12-16-npm-displays-packages-with-bundled-typescript-declarations/" rel="nofollow">https://github.blog/changelog/2020-12-16-npm-displays-packag...</a><p>JSDoc-typed node modules require special configuration in consumers to be useful <a href="https://github.com/microsoft/TypeScript/issues/19145" rel="nofollow">https://github.com/microsoft/TypeScript/issues/19145</a>
I'm a big advocate for explicit type annotations in code. Likewise, either or is good with me. However, these days I lean towards using JSDoc only because I prefer not to add typescript lib and an additional build step just to get type safety in a project.<p>Also, you can use both (if that's what you prefer).
This is how I develop web software, and I've found it productive and maintainable.<p>A bonus of this approach is that it clearly delineates what type information is available at runtime, vs what type information is just a comment. (especially useful when working with serialized data).<p>I also like that it generally means all of the preconditions and postconditions for functions are in a single place (both the prose and the technical information are in the JSDoc comment), keeping the code itself low-noise and boiled down to its essence, and providing a natural place to write example inputs and outputs to guide usage.<p>As for generic types, I use them extensively, and this is made less verbose e.g. on function calls by using an @type annotation on the function which accepts a TypeScript signature without needing separate @template + @param annotations.<p>It's awesome stuff, and I'm happy that TypeScript works so well with JSDoc!
Personally, given the limitations of JSDoc, I'd like to see a header-file like definition system for build-less applications (like libraries).<p><pre><code> /
index.js
index.d.ts
</code></pre>
Where values in `index.js` can be typed in the header file with complete TypeScript syntax and those types are present in the target file via intellisense and type checking<p><pre><code> // index.js
function useStr(x){} // has intellisense for "string"
useStr("Hello")
useStr(42) // IDE and type checker error
</code></pre>
And<p><pre><code> // index.d.ts
declare function useStr(x: string): void</code></pre>
This can be done, and is done in some projects, but the problem is that you need to manually maintain both sets of files, which can easily go out of sync if you're not careful. You also lose out on the ability to check your own code - using the header file you've written there, you can't really check the implementation of `useStr` to check that it works.<p>The advantage of JSDoc is that the TypeScript compiler treats it as equivalent to TypeScript source code, which means you've still got access to type checking inside your functions.<p>One thing I've seen in a few places is still to have DTS files that declare all sorts of types that are used in the application, and then have those files imported by JSDoc comments. That way, you've got TypeScript syntax for your types, which tend to get bulky and difficult to read in JSDoc syntax, but you still don't need a build system because the DTS imports are ignored completely at runtime.
You can define a declaration file alongside its target however the target does not use the types defined within its declaration within itself - only consumers see the types.<p>There are three issues with that.<p>The first is that JSDoc doesn't support everything you need in TypeScript and there is a lot of inlining (like typedef causes collisions, there's no importing a type without also re-exporting it from the importing file unless you inline the import)<p>The second is that JSDoc isn't picked up from imported libraries (or at least I don't think it is?)<p>Lastly, you still need a transpiler to remove the comments and build d.ts files.<p>In the end, JSDoc isn't practical for projects. The header file strategy means you don't need to transpile ever (only a typechecker) and you'd get the full suite of TypeScript functionality - at the cost of synchronization overhead.<p>For a project using JSDoc, you'll graduate to TypeScript when there is sufficient complexity anyway. Migrating from a d.ts file is way easier (potentially automatable) than migrating from JSDoc.
The problem with the header file strategy is that TypeScript doesn't have total type inference. That approach works e.g. in OCaml, where you can (optionally) type the boundaries of the function and everything inside the function can always be inferred. But TypeScript works differently and doesn't support that approach. So fundamentally, what you're describing isn't really possible without using a very restrictive subset of TypeScript that can be totally inferred or having very broad type definitions.<p>So even if TypeScript did check the header file against the implementation, you'd still need additional annotations inside the implementation file. At that point, why not put all the annotations inside the implementation file directly?<p>Regarding your points:<p>1. JSDoc does support everything that TypeScript supports (that's the point of this article), although it does not necessarily support it as cleanly, hence why projects like Svelte typically use DTS files to define the project's types, and have those be imported inside JSDoc annotations. It's not perfect, but it gets you a long way.<p>2. You're right, JSDoc isn't picked up from imported libraries, but if you're publishing a library, you'll have a build script there, and at that point it's typical to generate the DTS files and pack those with the source code. That seems fairly reasonable to me — while developing, you don't need to worry about building files, but then when packaging and releasing you do.<p>3. You don't need a transpiler with JSDoc, the point behind JSDoc is that it's literally just the pre-existing JS documentation syntax. You can (and should) leave the comments in. Even if you're using TypeScript syntax, you should have JSDoc annotations (although adding the types to those annotations is redundant in that case). You can also just include the DTS files. As long as they're only imported from JSDoc, the runtime won't see them.<p>Personally, having tried out JSDoc-based TypeScript, I'd rather just use the native type stripping in Node for development, and keep with TS syntax, but there are large projects that have made the opposite choice, most noticeably Svelte. So I don't think it's a given that JSDoc users will inevitably graduate to TypeScript (especially when projects have gone in the opposite direction).
I still think that JS is very much not TS. Most TS code assumes you never need to check for errors because the type checker proves they can't happen.<p>Then, paradoxically, with no error checking at runtime, it becomes fully possible for JS code to call into TS code in a way that breaks the shit out of the TS compiler's assumptions. In philosophy then TS and JS are as incompatible as GPL and EULA
It doesn't matter if a library is written in TS or JS; you cannot meaningfully protect against other code calling you incorrectly.<p>Sure, you can check if they gave you a string instead of a number. But if you receive an array of nested objects, are you going to traverse the whole graph and check every property? If the caller gives you a callback, do you check if it returns the correct value? If that callback itself returns a function, do you check that function's return type too? And will you check these things at every single function boundary?<p>This kind of paranoid runtime type-checking would completely dominate the code, and nobody does it. Many invariants which exist at compile-time cannot be meaningfully checked at runtime, even if you wanted to. All you can do is offer a type-safe interface, trust your callers to respect it, and check for a few common mistakes at the boundary. You cannot protect your code against other code calling it incorrectly, and in practice nobody does. This is equally true for JS and TS.
Writing a Typescript program that takes external input but has no runtime error checking is already a mistake, though. Dealing with external input requires type assertions (since Typescript doesn't know what the program is getting at compile-time) and if you write type assertions without ensuring that the assertions are accurate, then that's on you, not Typescript.<p>However, if your point is that Typescript can lull people into a false sense of safety, then sure, I take your point. You have to understand where type assertions are coming into play, and if that's obscured then the type safety can be illusory. The benefits of Typescript require you to make sure that the runtime inputs to your program are sufficiently validated.
> Writing a Typescript program that takes external input but has no runtime error checking is already a mistake, though.<p>If it's a service, yes, and that's true no matter what technology the service is using. If it's a <i>library</i>, no, because...<p>> and if you write type assertions without ensuring that the assertions are accurate, then that's on you, not Typescript.<p>That's on whoever is using the library, not the library author. If the library author provides type definitions, and you as the consumer choose to ignore them, then it's on you.
TS certainly thinks of external input as a boundary requiring safety, but usually that would mean form input or CLI args parsing or something.<p>Usually there's little if any protection against a JS caller providing wrong-type args to TS functions.
Sure, but if the caller is Javascript then you're running Javascript, not Typescript*, so it makes sense that you're not going to get type safety.<p>I'm also not sure we're actually disagreeing on anything, so perhaps my reply was pointless. I agree that if you mix JS and TS in the way you describe, you'll have problems. My reply was just to say "yes, and that's not really Typescript's fault", but perhaps you weren't implying that to begin with.<p>* I'm aware that you can't run Typescript directly, but I hope my point here is clear... you're running a program that wasn't type-checked by TS.
Something similar is true for most statically typed languages.<p>If you write a C library, nothing stops someone from writing an assembly-language program that calls functions in your library with the wrong types.
The type checker can only prove what is known at compile time and only if you're disciplined.<p>To bridge runtime and compile time (as your application will likely get some external data) you've got to use a proper parser such as zod[1] or if you want to stretch it even further effect-schema[2].<p>[1] <a href="https://zod.dev/" rel="nofollow">https://zod.dev/</a><p>[2] <a href="https://effect.website/docs/schema/introduction/" rel="nofollow">https://effect.website/docs/schema/introduction/</a>
A somewhat related thing programmers must understand is that whether you write typescript, JSX, .astro or .svelte files, you are technically not writing JavaScript.<p>You should occasionally look at the build artifacts of your framework but also ask yourself whether it is worth it to write code that may not represent what actually ends up being executed.<p>Lately I just use vite with no starter template but with web components and css modules. It at least feels more convenient than using any framework or library.
This seems like an issue but in all my practical experience it really isn't. TypeScript becomes JavaScript with the types removed. Then you tree-shake, minify, and whatever is executed is no where near what you actually wrote but at the same it totally is the same because that's no different than any other compilation process.<p>Occasionally, I have to remember that JavaScript has no types at runtime but it's surprisingly not that often.
I mean what makes it more acceptable is that you have HMR and instant preview during development. So, all the transformations and bundling aside, you do see how it runs during development.<p>However I have been in a few situations at work where all of a sudden we did have issues that required us to dig deeper. There were also some bundling related issues that caused problems in production deployments. At that point many co workers had no idea how to even approach it to be honest.
I've had similar experiences with compiled languages in one form or another throughout my career as well. I don't think JavaScript is particularly special that we need to call it out for things like TypeScript, minification, or bundling.
> ask yourself whether it is worth it to write code that may not represent what actually ends up being executed.<p>Doesn't this describe every programming language?<p>When you write C, you are technically not writing machine code.<p>Even when you write JavaScript, what actually gets executed is V8 bytecode and/or machine code (depending on whether the JIT fires).
That's correct, however I would say there is a small difference in that most of this code still <i>seems</i> just like JavaScript, sometimes it even feels as though it is JavaScript running in the same context when it then gets compiled to run on server/client.<p>I think the point I'm trying to make is that this can be confusing or even dangerous especially for new developers. It just doesn't hurt to actually look at the Vite plugins transforming it all to understand it instead of making assumptions if we work with it on the daily.
Yeah it’s a silly line of reasoning. The transformations of TS -> JS are a lot smaller and simpler than C-> asm / machine code; it’s basically just removing type annotations. Now minification and optimization can make the output a lot more terse, but that can be done for JS too. And it’s not as complicated and detached from the source as an optimizing compiler is.
Let's not act like it's the same thing. I'm not strictly talking about just Typescript, I'm saying that if you work with these technologies every day it would be wise to go look at their Vite plugins to see how they transform your code and be sure to understand it. It's nice to have <i>magic</i> but it's nicer to use the magic if we have demystified it first.<p>And I don't know about you, but I occasionally do open compiled ELF files in a hex editor and I certainly did at first when I was learning more. That's a good practice also.
Aren't you loosing a lot of the declarative features like signals or similar, when you do your projects without those frameworks?<p>(asking to learn)
Somewhat. I could still use framework agnostic state management libraries/patterns and most are (e.g. svelte signals, jotai, zustand, etc.).<p>I've even used Proxies directly to implement some reactivity before. However as for the "declarative" parts, I think it's just a little bit of a different way to work but you get used to it and imo it pays off. Knowing the web APIs <i>should</i> be a requirement anyway and it doesn't hurt to work with them directly as much as possible.
What I would really like to see is a TS compiler that emits .js libraries with typing information compiled as JSDoc comments.
Webpack is typed using JSDoc and type-checked via TypeScript -- I started this migration a while ago. It works pretty well
Aside from the following, JSDoc is absolutely TypeScript.<p>Discriminated unions, conditional types, mapped types, template literal types, recursive type aliases, higher-kinded type patterns, generic constraints with conditional inference, the infer keyword, never type exhaustiveness checking, const assertions, readonly tuple and array inference, exact object types, key remapping in mapped types, indexed access types, variance annotations, assertion signatures, strict null checking with flow-sensitive narrowing, definite assignment assertions, the satisfies operator, declaration merging, module augmentation, symbol-typed properties, type-safe enums<p>…and not written via comments
Yes, all of these are supported in JSDoc because they are supported in TypeScript. Because JSDoc is TypeScript. You can either define your types in JSDoc comments or in .ts files.<p>I really mean it. You can even use the @satisfies operator like so<p><pre><code> /** @satisfies {Type} */
</code></pre>
Discriminated unions, conditional types, mapped types, template literal types, etc work exactly the same way if you define them in a JSDoc comment as if you define them in a .ts file
Sounds like Scala type fetishism all over again.
jsdoc is nice because you don’t have to write the non-helpful types.<p>---<p>In WebStorm, jsdoc can be rendered in HTML, which makes the code easier to scan. Here's a side-by-side VSCode vs WebStorm:<p><a href="https://x.com/efortis/status/1989776568676221137" rel="nofollow">https://x.com/efortis/status/1989776568676221137</a><p>---<p>And in jsdoc you can have an inline description:<p><pre><code> @prop {number} width Video width in pixels</code></pre>
Oh man, the mention of ScriptSharp brought back memories. I started my career at MSFT on SharePoint and the front end was an ungodly mix of ScriptSharp and other stuff.<p>I vividly remember being in a meeting with the Exchange team (about building shared frontend components) arguing for us to adopt TS instead as it had a better experience and very rapidly growing popularity (that was about 10 years ago). Plus, as strong as Nikhil [0] was, he was basically the only person behind ScriptSharp while TS had a whole team.<p>Of course, this being MSFT, this effort went no where. While true that the TS toolchain lacked the tree-shaking that ScriptSharp had, I was just annoyed that we had to build stuff using what was obviously an dead-ish language with limited support, many flaws, and no resources to improve it.<p>But hey, at least it wasn’t GWT.<p>[0] <a href="https://github.com/nikhilk" rel="nofollow">https://github.com/nikhilk</a>
From what I've read, many of TypeScript's design regrets have political origins. Enums and other features that oppose TS's structural type system were added as compromises with C# developers in MS and similar negotiations with the Angular team in order to increase adoption of TypeScript over alternatives
Not really, at best it's a verbose and limited subset.<p><a href="https://github.com/tc39/proposal-type-annotations?tab=readme-ov-file#limits-of-jsdoc-type-annotations" rel="nofollow">https://github.com/tc39/proposal-type-annotations?tab=readme...</a>
It's certainly more verbose but certainly not a limited subset. You can copy paste any typescript Type or Interface in a jsdoc @typedef. As I stated in the article, it's still the TypeScript language service that's analyzing either JSDoc-defined types or TypeScript defined types. It's typescript all the way down<p>But JSDoc lets you do pretty much everything that isn't a runtime feature of TypeScript (e.g. enums, namespaces, etc). Even generic slots are supported
Although it's more verbose, it makes code less dense because it lives outside the function parameters.<p>Also, it's not a super limited subset. For instance, you can write types in .d.ts the IDE uses those types in jsdoc out of the box.
The list of things jsdoc cannot do is long but a simple example is overloading, you cannot express this[1] in jsdoc, and if you need to reference .d.ts files you're back at using TypeScript, so the point of JSDoc was...?<p>If you need precise return typing, conditional types, literal values, etc, you aren't going far if anywhere with JSDoc.<p>[1] <a href="https://shorturl.at/pg5dL" rel="nofollow">https://shorturl.at/pg5dL</a>
There a few points in favor of TS types in jsdoc (in order):<p>1. not having to type everything (many types are not helpful)<p>2. makes code less dense (and they can be rendered in HTML)
<a href="https://x.com/efortis/status/1989776568676221137" rel="nofollow">https://x.com/efortis/status/1989776568676221137</a><p>3. not needing a compiler (browsers don't support TS)<p>I think the main jsdoc caveat is around private types.<p>About 1, IDK if there’s a way to set up TS for that.
This specific one is actually supported since TypeScript 5.0:<p><a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-0/#@overload-support-in-jsdoc" rel="nofollow">https://devblogs.microsoft.com/typescript/announcing-typescr...</a><p>But not properly documented (<a href="https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html" rel="nofollow">https://www.typescriptlang.org/docs/handbook/jsdoc-supported...</a>), which shows how much Microsoft neglects JS + JSDoc workflow. Github issues have been created a long time ago, but nothing has been done so far. Apparently Microsoft is too busy with their AI slop to work on actually useful stuff.
Some TypeScript features are <i>only</i> available through JSDoc. The one I encounter most often is `@deprecated`.
No it isn’t, otherwise Closure Compiler would (still) be running MS365
Eh. I agree with the principle. I’ve written personal projects with JSDoc because I truly love the idea of finally being done with build systems and just serving the files I write without a step in between.<p>But it’s more annoying than just writing TypeScript. There are ways to express just about everything TypeScript can but they’re all more difficult and convoluted (generics are a great example). For a project of any reasonable size I’m still going to advocate to use TypeScript.
I am always using JSDocs whenever i am writing a function - i think this is a good practice for every developer - even if it's a simple function.
I like the mention to someone from the React team as it seems TypeScript/type safety did not help them create better, safer software.
And in fact, this what the Closure Compiler does…typecheck based on JSDoc.<p>However, the precision and completeness is not nearly what can be expressed in TypeScript. With generics particularly.
Agree. It’s superior. I arrived at this about 2 years ago no regrets. Type safety matters on the tooling side anyway. Unless you’re testing for the runtime I guess?
The sooner we get <a href="https://github.com/tc39/proposal-type-annotations" rel="nofollow">https://github.com/tc39/proposal-type-annotations</a>, the better.<p>Once we get it, there is still a solid decade before runtimes support it, and optimistically, still more 10 years minimum having to deal with an interpreted language that has acquired an unecessary build step.<p>I absolutely hated when PHP switched from a phpDoc culture with static analysis (and IDE inconsistencies that would click-take you to stubs as well) to actual types. Not because I hate types, but because of the transition period. Once it's gone, it's such a relief to get rid of unecessary docblocks.
[dead]
[flagged]
Who cares what some framework guy thinks. When I was writing JavaScript for employment most people doing that work were hyper concerned with how to write code and what other people thought about it. These kinds of opinions and conversations are critically important for beginners, but junior and senior developers never seemed to get past these concerns of basic literacy.<p>When other developers and non-developers look at JavaScript developers as small children it’s because the maturity difference is very evident from the outside. Once developers get past basic literacy they are free to worry about architecture, performance, scale, platform independence, and more. For most JavaScript developers they just expect some framework to do it for them.