Is it possible to automatically convert JavaScript into TypeScript? [closed]
Asked Answered
J

2

5

I wrote a game using coffeescript but I would have preferred to write it in typescript. The brute force way to resolve this is to convert the code by hand. But I'm wondering if there's a way to take the coffeescript or the javascript generated by coffeescript and convert it into typescript automatically. A man can dream.

Janelljanella answered 18/6, 2013 at 20:29 Comment(1)
You can try using npmjs.com/package/coffee-script-to-typescript!Gland
C
7

JavaScript code is already TypeScript code. Although it may have type errors, the compiler will still output a file (when it says 'error' it really means 'warning' unless the error occurred during parsing, which shouldn't be the case for JS that actually runs).

In terms of converting idiomatic JavaScript class/module-like structures into TypeScript classes or modules, there aren't any tools for that (yet). The TFS Team wrote an internal bespoke tool for their codebase to do this, but that was only really possible because their JS was written under very strict rules to begin with.

Catfall answered 18/6, 2013 at 20:38 Comment(2)
this is the best answer.Meteoric
+1 - paste your JavaScript into a TypeScript file and start massaging it!Gratuity
M
2

This sounds like a bad idea. Even though they both produce JavaScript, CoffeeScript and TypeScript are very different. However, since TypeScript is a superset of JavaScript, the transpiled CoffeeScript is also valid TypeScript. From Wikipedia:

TypeScript is a superset of JavaScript. By default the compiler targets ECMA Script 3 (ES3) but ES5 is also supported as an option. A TypeScript application can consume existing JavaScript scripts. Compiled TypeScript scripts can be consumed from JavaScript.

And since you don't seem very committed to your "flavour of the week compiles to JS language" of choice why not actually write it in JavaScript? Just as an academic exercise?

Melvinamelvyn answered 18/6, 2013 at 20:36 Comment(3)
Honestly, I liked coffeescript more than JS because it's more concise. I would have used typescript from the get-go, but I didn't think my IDE supported it. I figured the productivity gains from type checking would be negated by productivity losses of using a new editor. Turns out my IDE does have typescript support after all, and now I'm thinking about making the switch.Janelljanella
Personally I wouldn't make IDE support the deciding factor. Where CoffeeScript is more concise TypeScript seems to be the total opposite (static typing, verbose meta comments etc).Melvinamelvyn
Just to add new context to a years-old POV, "flavour of the week" is a bit harsh on TypeScript these days, since TypeScript is now the parent language of AngularJS 2.0Semasiology

© 2022 - 2024 — McMap. All rights reserved.