Skip to content

Limit JSDoc typedefs to file, opt-in to exporting them #43207

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

Closed
5 tasks done
thesoftwarephilosopher opened this issue Mar 12, 2021 · 3 comments
Closed
5 tasks done

Limit JSDoc typedefs to file, opt-in to exporting them #43207

thesoftwarephilosopher opened this issue Mar 12, 2021 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@thesoftwarephilosopher
Copy link

Suggestion

πŸ” Search Terms

jsdoc typedef export

βœ… Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Keep @typedef and @type local to file (or scope?) unless exported (at top-level of file).

πŸ“ƒ Motivating Example

I've come to find JSDoc + VS Code the best way to use TypeScript:

  • There's no compile-phase
    • Code is always valid as-is
    • Forgetting to build isn't fatal
    • Often vastly simplifies tool chain
  • You just use it within VS Code
    • Let it show in-IDE errors
    • Let it install/run TypeScript for you
    • Still provides autocompletion and everything
  • Encourages avoiding insanely complex types
    • You can still make complex types via index.d.ts if really necessary
  • You usually already want to document function parameters anyway, might as well use JSDoc
  • Function declaration lines don't end up being 500 characters, since you can put each parameter on each line

It's overall a pretty good alternative to TypeScript in small/medium projects.

But it has this one big caveat: All types/typedefs in any files are visible in all files.

  • This means you can't cleanly have helper types encapsulated to a file.
  • And you can't mark types as being exportable, which means you can't have multiple types with the same name.

Fixing this would make JSDoc an extremely useful way to use TypeScript with the "almost zero config" VS Code approach.

(There's also #42949 which is unrelated to this except that it's one small but fixable thing making JSDoc support not as useful as TypeScript syntax.)

πŸ’» Use Cases

^

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Mar 12, 2021
@thesoftwarephilosopher
Copy link
Author

Never mind, looks like this is already how it behaves. Not sure why I thought otherwise.

@kungfooman
Copy link

Never mind, looks like this is already how it behaves. Not sure why I thought otherwise.

They will definitely be exported as you can see from this error: #46011 (comment)

Anyone happens to know which PR changed this behaviour then? That would be a starting point to fix the behaviour, personally I would like a possible @private field on @typedef's.

@ljharb
Copy link
Contributor

ljharb commented Oct 5, 2023

I'm running into this now; I have a CJS file with JSDoc comments that's generating types, and the resulting types have export = (as they should) for the CJS value, and also export type for the typedef, despite not explicitly exporting it.

This should be reopened.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants