logoalt Hacker News

culilast Sunday at 10:31 PM5 repliesview on HN

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.

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:

  /** @type {ReturnType<typeof useState<Book[]>>} */
  const [books, setBooks] = useState();

Replies

g947olast Sunday at 11:18 PM

Last time I checked, there isn't a way to extend types (https://www.typescriptlang.org/docs/handbook/2/objects.html#...) that works exactly like in TypeScript.

show 3 replies
mirekrusinyesterday at 10:13 AM

Things like type star imports, destructured imports or satisfies operator don't work in jsdoc.

All non erasable constructs won't work as well of course but playing devil's advocate you could explicitly state that you're interested in erasable constructs only because ie. 1) that's what typescript should be doing from day 1 and/or 2) it seem to be the future with ie. nodejs adopting built in type erasure support.

g947olast Sunday at 11:14 PM

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.

show 1 reply
efortislast Sunday at 10:36 PM

and types can be written in .d.ts file(s), which can be used in jsdoc out of the box (no import needed)

show 1 reply
afavourlast Sunday at 11:06 PM

ReturnType is TypeScript, no? You’re using JSDoc to express but it’s a TypeScript type.

show 2 replies