Skip to content
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

Support 'Promote JSDoc comment to type annotation' #2916

Closed
2 of 5 tasks
danquirk opened this issue Apr 24, 2015 · 9 comments
Closed
2 of 5 tasks

Support 'Promote JSDoc comment to type annotation' #2916

danquirk opened this issue Apr 24, 2015 · 9 comments
Labels
Duplicate An existing issue was already created Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@danquirk
Copy link
Member

We should consider the feasibility of a simple editor command where existing JSDoc comments are automatically converted to TypeScript type annotations. This could be a massive benefit for easing migration from JavaScript to TypeScript both in terms of saving pain fixing 'errors' (ie places that need type annotations) and being able to immediately leverage TypeScript's biggest value prop.

In addition, this could be a great way to improve the .d.ts generation process. Imagine picking up a new JS library that doesn't have a .d.ts but is well documented with JSDoc comments. Despite the lack of a .d.ts on DefinitelyTyped you could simply use this new command to promote the JSDoc comments to type annotations, then run the file through tsc --declaration and in almost no time have a high quality .d.ts.


Edit by @DanielRosenwasser:

  • Teach the parser how to parse full JSDoc format.
  • Emit JSDoc in .js files
  • Display a warning when users use a JSDoc type annotation instead of a TypeScript type annotation.
  • Support a tool/mode to generate a .d.ts from a .js file
  • In the language service, JS doc could be used to infer types for .js files.
@danquirk danquirk added the Suggestion An idea for TypeScript label Apr 24, 2015
@mhegazy
Copy link
Contributor

mhegazy commented Apr 25, 2015

I like the idea; but I thik we should break this into pieces:

  • Teach the parser how to parse full jsdoc format.
    This is full fledged support; parse diffrent variants, report diagnostics for wrong syntax... etc.. We sold report errors for param documentation that does not match signatures for instance; possibly as a warning.
    This also allows us to do better jsdoc intellisense, rename documentation when we rename declaration, atomatically creat doc comments and fix issues with doc comments.
  • in typescript files, it would be an error to add type annotations in jsdoc for Parameters or return types.
    We can then add a light pulp quick fix to "add a type annotation". And this would be your migration story. Start with documented js code run a quick fix and voila! You have ts code.
  • emit jsdoc in js files
    Users have asked for this multiple times. If we are parsing and understanding jsdoc, it should be trivial to fill in type information and generate full fledged Jsdoc for output js, we would do that under a --generatJSDoc flag. This also allows other tools in the tool chain, e.g. Closure compiler to do better optimizations.
  • support a tool/mode to generate a .d.ts from a .js file
    This would be built on our support for jsdoc, and use these to generate types as good as it can, along with possibly tweeked inference algorithms to get you started. I believe it is important to treat this as a separate step to allow users to twerk the resulting .d.ts after the initial generation as the process is inherently lossy and we have no way of doing it right all the time.
  • finally, in the language service if you are in a is file, jsdoc would be used to infere types.
    I believe this is not contravetial and fits with the migration story stated above.

@teppeis
Copy link

teppeis commented Apr 26, 2015

support a tool/mode to generate a .d.ts from a .js file

I created closure-ts that is d.ts generator from JSDoc typings.
It generates closure-library.d.ts from Closure Library that is fully typed with JSDoc annotation.

@danquirk
Copy link
Member Author

I'm unsure whether we would want to do something like give an error on certain JSDoc comments in TypeScript. It seems plausible that people would still want those comments for some other system in their build pipeline but perhaps not.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 27, 2015

@danquirk that is why i was saying 1. make it a warning not an error, and 2. emit type information in jsdoc. so it is an error to include the type in a .ts file jsdoc, and the compiler will put in the correct type when it writes out the jsdoc.

@DanielRosenwasser
Copy link
Member

I adapted @mhegazy's list to a task list in the original comment. @CyrusNajmabadi can check off anything he's already worked on. I think the first item has been taken care of.

@danquirk danquirk added the In Discussion Not yet reached consensus label Jul 22, 2015
@aozgaa
Copy link
Contributor

aozgaa commented Jul 26, 2015

Regarding the first step, it appears https://github.com/jsdoc2md/jsdoc-parse already provides some of the facilities for parsing JSDoc, though it additionally gets type information from declaration signatures. Perhaps rather than re-writing the tool for our own purposes we could contribute and add a PR for an API?

EDIT: using that tool would likely be inefficient as it requires a second scan of a sourcefile for the tool and it's unclear how to extend it for live-editing. oops.

@rgbkrk
Copy link
Contributor

rgbkrk commented Jul 26, 2015

Perhaps rather than re-writing the tool for our own purposes we could contribute and add a PR for an API?

That would be so great to see adoption within the community. 😄

@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this and removed In Discussion Not yet reached consensus labels Aug 5, 2015
@RyanCavanaugh RyanCavanaugh added this to the Community milestone Aug 5, 2015
@RyanCavanaugh
Copy link
Member

Lots of work to do here. @aozgaa is doing some of it; we'd take PRs on other parts. This can be an area of incremental improvement

@mhegazy
Copy link
Contributor

mhegazy commented Feb 22, 2016

Most of the work outlined here has been covered by #4789

@mhegazy mhegazy closed this as completed Feb 22, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 22, 2016
@mhegazy mhegazy removed this from the Community milestone Apr 26, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants