-
-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: produce TS declaration files #317
Conversation
Thanks for your interest in contributing to the project! For a long time (over a year) I investigated the best possible way to add type safety to all my packages that works universally in Node.js TypeScript projects as well as Deno projects, without a build step. I consciously decided generating type definition files was not part of the solution, for reasons (briefly) explained here: JavaScript modules containing TypeScript JSDoc comment based types are incredibly portable. Deno users can import them from almost any CDN and enjoy the types without doing anything special, and it's really easy for Node.js TypeScript users to tweak their TypeScript config to get a similar experience. |
The "build step" is already in the repo and part of your Requiring users to change away from the default |
The situation is more nuanced than you may realise. For example, what happens when a Deno user imports a module from a package that has both JSDoc types and type definitions (like proposed by this PR) via a CDN like esm.sh, that detects there are type definition files, does a bunch of work to resolve and build Deno friendly type definition files for that version of the package if it is not yet built, and then adds them in the response in a There are more things to consider I haven't even mentioned yet. For example, when using JSDoc based types command clicking on them in your project code opens the actual module you are using them from and takes you to the right place, instead of opening some definition file that is missing all of the context the actual implementation code gives you. If we added definition files on top of the JSDoc comments, we would lose that DX. The way my package |
Silver bullets are rare, and in this instance I feel the sheer number of TS related comments in many of the projects issues regarding the types (not) exported by this package, tells the true story. JSDoc is a documentation tool, a great one, but it's only that. It can't handle complex generic types, higher kinded types, conditional types, type narrowing, I could go on; ergo, choosing it as the single option to document all of your packages seems a little ambitious. I've been down this road before with many small packages, and have almost always ended up following the popular maintainers approach of writing JS and "hand crafting" TSD files on the side. They sit right next to the files that they describe (avoiding the issue of context that you rightly mentioned), and do a much better job describing the type level information than JSDoc could ever do on its own. This approach requires no tooling, no extra build steps, is easy to maintain, and future proofs your project in the event that you require any features that JSDoc simply cannot handle. I'm seeing this package bundled in with many Apollo codebases these days, and I really think that they would all benefit from either this PR, or a straight up rewrite of the types as raw TSD files. |
This allows consumers not to set
allowJs
andmaxNodeModuleJsDepth
in order to consume the types