js-to-ts-converter icon indicating copy to clipboard operation
js-to-ts-converter copied to clipboard

type inference?

Open cancerberoSgx opened this issue 5 years ago • 1 comments

I'm not seeing the tool to infer types form usage.. And IMO this should be the basic feature for a tool like this. I see you have focused more on code tranpilation like class declaration synyax, I think the most important part is being able to add acceptable types to variables , parameters, return types, etc (and not just add "any" to everything. So I wanted to give you a tip:

Fortunately the compiler language service is capable of inferring times for most of variables, parameters, etc. I found out it has LOTS of code-fixes that you can apply programatically. These are some examples (using ts-morph) library which provides an higher API, but it's also accomplished with compiler API directly:

This is a typescript-plugin that applies / suggests all codefixes available in the file tying suggestions in all ranges: https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/typescript-plugin-all-ts-fixes-and-refactors/src/supportedCodeFixes.ts

You might find interesting this snippet that prints all code-fixes and refactors codes, names and descriptions:

https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/typescript-plugin-all-ts-fixes-and-refactors/src/supportedCodeFixes.ts

This one has a inferTypesFromUsage() function that is what I just suggested to add in your lib:

  • https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/ts-simple-ast-extra/src/refactor/refactors.ts

convert require to import:

  • https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/ts-simple-ast-extra/src/refactor/convertImports.ts

these is a test that runs the for remove all unused variables ode fix:

  • https://github.com/cancerberoSgx/typescript-plugins-of-mine/blob/master/ts-simple-ast-extra/spec/refactors/removeAllUnusedSpec.ts

Hope it helps! feel free to use the library or code. thanks! keep ip up!

cancerberoSgx avatar May 23 '19 19:05 cancerberoSgx

An awesome idea. Wish I had any time to implement this 😂 PRs welcome!

gregjacobs avatar Sep 08 '20 04:09 gregjacobs