Skip to content

Support 'Promote JSDoc comment to type annotation' #2916

Description

@danquirk

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 Daniel Rosenwasser (@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.

Activity

  1. mhegazy commented on Apr 25, 2015

    @mhegazy
    Contributor

    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.
  2. teppeis commented on Apr 26, 2015

    @teppeis

    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.

  3. danquirk commented on Apr 27, 2015

    @danquirk
    MemberAuthor

    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.

  4. mhegazy commented on Apr 27, 2015

    @mhegazy
    Contributor

    Dan Quirk (@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.

  5. DanielRosenwasser commented on Jul 22, 2015

    @DanielRosenwasser
    Member

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

  6. aozgaa commented on Jul 26, 2015

    @aozgaa
    Contributor

    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.

  7. rgbkrk commented on Jul 26, 2015

    @rgbkrk
    Contributor

    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. 😄

  8. added this to the milestone on Aug 5, 2015
  9. RyanCavanaugh commented on Aug 5, 2015

    @RyanCavanaugh
    Member

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

  10. mhegazy commented on Feb 22, 2016

    @mhegazy
    Contributor

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

  11. removed this from the milestone on Apr 26, 2018
  12. locked and limited conversation to collaborators on Jul 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already createdHelp WantedYou can do thisSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions