13 ms·
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
by mirekrusin 11mo ago
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
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)
- measurablefunc 11mo agoTypeScript's type system is Turing complete so you have access to essentially unlimited expressivity (up to the typechecking termination depth): https://news.ycombinator.com/item?id=14905043 https://news.ycombinator.com/item?id=14905043
- creatonez 11mo ago> there are plenty things you can't express in jsdoc but can in typescript This isn't really true anymore, they have systematically added pretty much every type system feature to the JSDoc-like syntax.
- g947o 11mo agoHaving JSDoc-like syntax isn't the same as it being fully supported. If you have a large enough codebase, you'll likely find a few cases where things work in TypeScript but its equivalent somehow fails type check in JSDoc.
- trekz 11mo ago> If you have a large enough codebase, you'll likely find a few cases where things work in TypeScript but its equivalent somehow fails type check in JSDoc. You keep repeating this throughout the thread. Can you give an example?
- g947o 11mo agohttps://news.ycombinator.com/item?id=46268701 https://news.ycombinator.com/item?id=46268701
- spartanatreyu 11mo agoNot that person, but is there an easy way to write something like a basic semver? export type SemVer = `${number}.${number}.${number}`; Could you extend it to work with regex groups like: export const SemVerRegex = /^(?<major>0|[1-9]\d)\.(?<minor>0|[1-9]\d)\.(?<patch>0|[1-9]\d)(?:-((?:0|[1-9]\d|\d[a-zA-Z-][0-9a-zA-Z-])(?:\.(?:0|[1-9]\d|\d[a-zA-Z-][0-9a-zA-Z-]))))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/; Could the groups be extracted so you the type back if you ran the regex on a string like: "1.2.3", or "1.2.3-prerelease"?
- g947o 11mo agoit is possible to do many of these with @typedef, but it gets difficult with JSDoc very quickly. In TypeScript you can easily create multi-line type aliases. Not quite so in JSDoc.
- matt_kantor 10mo agoI don't understand the second part of your comment (that's a value, not a type), but the first part looks like this: /** @typedef {`${number}.${number}.${number}`} SemVer */ Here's a playground: https://www.typescriptlang.org/play/?filetype=js#code/PQKhAIAEBcE8AcCmATRAzcBvABgEkwHYCuAtgEaIBOAvgHT7HlV0OkU3bXgDKiJAalXAhgAKFGgIMBIiy8BzYWIDGAewIBnaOFUBrcAF5wAcgCMtAEy0AzMfGSocJHL6CaS0Ws3ayAQ2SGJubQAO6qNsZAA https://www.typescriptlang.org/play/?filetype=js#code/PQKhAI...
- spartanatreyu 10mo agoThanks for the example, I had a good play around with it. The second part of my comment is a value yes, but it's implicitly typed by typescript automatically. I was asking about how to use that type (and it's internals) in jsdoc.
- culi 11mo agoWell 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();
- efortis 11mo agoand types can be written in .d.ts file(s), which can be used in jsdoc out of the box (no import needed)
- g947o 11mo agoYou could, but in a large codebase with multiple contributors, it easily becomes messy and confusing.
- mirekrusin 10mo agothat's not "jsdoc is typescript" anymore because you're falling back to something else - type definition files where you do have access wider typescript functionality. also not a full solution - for .d.ts types to be available globally without explicit import the .d.ts file itself cannot use any imports/exports. this means you can't reuse types from other places to construct your types. you can workaround this by explicitly importing .d.ts in jsconfig/tsconfig but you're still left with other issues. those types do actually become globally visible everywhere polluting global namespace which is bad in itself there are no guarantees about them being in sync with actual code, which violates the whole point of using type safety. they don't solve cases where you need typescript inlined functionality locally in your code or to perform assertion with satisfies operator etc.
- afavour 11mo agoReturnType is TypeScript, no? You’re using JSDoc to express but it’s a TypeScript type.