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

Feature: migration tool to convert a full project from JSDoc to TypeScript strict #19285

Closed
cyrilletuzi opened this issue Oct 18, 2017 · 16 comments
Labels
Community Tooling This might be better handled by tooling from the community instead of built into TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@cyrilletuzi
Copy link

cyrilletuzi commented Oct 18, 2017

Currently, to stay aligned with native JavaScript, you can migrate a JavaScript project to TypeScript just by switching to .ts files and everything should be OK (except if there are real errors).

But you're not in strict mode, which is what you want if you come to TypeScript.

Now that JSDoc is understood by TypeScript, and following #18747, it would help TypeScript adoption a lot to have a tool to refactor a full JavaScript project, correctly commented with JSDoc, to TypeScript (applying #18747 quickfix everywhere).

It could even include the conversion from .js to .ts extension.

It would mean we can automatically migrate from a JS project to real TypeScript (aka strict mode).

@kitsonk
Copy link
Contributor

kitsonk commented Oct 18, 2017

Sounds like a perfect idea for a wider community project that leverages the TypeScript language services and compiler API. Building it into tsc doesn't feel in line with the design goals IMO.

@sandersn
Copy link
Member

👍 We would love to see a separate tool that does this. Typescript may add individual features over time to make it easier, but I think a "js2ts" tool is the right design.

@mhegazy mhegazy added Suggestion An idea for TypeScript Community Tooling This might be better handled by tooling from the community instead of built into TypeScript labels Oct 18, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Oct 18, 2017

We have made sure that all our codefix/refactoring logic is in a detachable form the language service API*. so it should be possible to leverage all the refacorings logic to achieve a standalone conversion tool.

(*) i am sure we will find bugs once we try to use them in a different context, but these should all be fixable.

@DanielRosenwasser
Copy link
Member

Related is #19255

@stereobooster
Copy link

I have idea for another tool (but related) generate type definitions from js code annotated with JSDoc, but type checked with TypeScript. Example puppeteer: they use ES7 + JSDoc + TypeScript as a a checker. Related: puppeteer/puppeteer#1170

@mhegazy
Copy link
Contributor

mhegazy commented Oct 30, 2017

I have idea for another tool (but related) generate type definitions from js code annotated with JSDoc, but type checked with TypeScript.

Looking for #7546 ?

@stereobooster
Copy link

@mhegazy yes, thank you very much

@cyrilletuzi
Copy link
Author

Closing this as it has been told as outside TS goals.

@mhegazy mhegazy reopened this Feb 8, 2018
@mhegazy
Copy link
Contributor

mhegazy commented Feb 8, 2018

It is out of scope of the TypeScript project, but we like to keep the suggestion active; a community member can pick this one up and provide a tool that accomplish the task. we would be happy to help as needed.

@weswigham weswigham added Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. In Discussion Not yet reached consensus labels Nov 6, 2018
@RyanCavanaugh RyanCavanaugh removed In Discussion Not yet reached consensus labels Mar 7, 2019
@cyrilletuzi
Copy link
Author

Closing this issue as TypeScript now knows how to convert jsdoc to types for a whole file at once.

@fungiboletus
Copy link

This issue appear on search engines when you look for that.

How can you convert a .js with JSDoc comments to a .ts file ? I cannot find how in the documentation. It's recommended to rename the file and fix it manually.

@karlhorky
Copy link
Contributor

Yeah I would like to be able to do this too - I would like a codemod or similar to be able to transition from this:

function add (/** @type {number} */ a, /** @type {number} */ b) {

...to this:

function add (a: number, b: number) {

...as well as supporting all other features of JSDoc (eg. @typedef -> type alias, @template -> generics, etc).

I assume such a converter / codemod exists somewhere, but I haven't been able to find it yet...

@danielo515
Copy link

If anyone ever finds such codemod, or builds it, please come back here and let us know

@whocaresk
Copy link

If anyone ever finds such codemod, or builds it, please come back here and let us know

https://www.npmjs.com/package/ts-migrate-plugins
this is probably what you're looking for

@karlhorky
Copy link
Contributor

@whocaresk do you know how well this works? How many different sorts of JSDoc annotations can it convert to TS?

cc @edsrzf

@sandersn
Copy link
Member

There's also https://github.com/microsoft/ts-fix, which should let you run the jsdoc-to-types codefix on your whole project at once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Tooling This might be better handled by tooling from the community instead of built into TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests