Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Typescript to Coffeescript

Tags:

I have a build chain setup that will convert a file from coffeescript to typescript to javascript. My question is: what is the most minimally intrusive way to add type signatures to a coffeescript function?

coffeescript supports raw javascript through backticks. However, that means coffeescript no longer understands the backtick snippet.

Coffeescript rejects these:

f = (`a:String`) -> a + 2
f = (a`:String`) -> a + 2

I can write this above the function:

`var f = (String) => any`

It compiles, but does not do the type-checking. I think this is because Coffeescript already declared the variable.

The only way I could figure out how to make it work requires a lot of boilerplate

f = (a) ->
  `return (function(a:String){`
  a + 2;
  `})(a)`

Backticks do not seem to work properly in the new Coffeescript Redux compiler: https://github.com/michaelficarra/CoffeeScriptRedux/issues/71

I am well aware that this is a dubious endeavor, it is just an experiement right now. I currently use contracts.coffee, but I am looking for actual types.

like image 391
Greg Weber Avatar asked Oct 13 '12 17:10

Greg Weber


People also ask

Is TypeScript and CoffeeScript the same?

Bottom Line. One crucial difference between the two languages is that TypeScript is the superset of JavaScript while CoffeeScript is a language which is an enhanced version of JavaScript. Not just these two languages but there are other languages such as Dart, Kotlin, etc. which can be compiled into JavaScript.

Is CoffeeScript obsolete?

As of today, January 2020, CoffeeScript is completely dead on the market (though the GitHub repository is still kind of alive).


2 Answers

Here's my project which transpiles CoffeeScript into TypeScript and then merges it with a d.ts file containing types. Then reports compilation errors, if any.

Its called Compiled-Coffee.

like image 193
Tobias Cudnik Avatar answered Sep 28 '22 06:09

Tobias Cudnik


If you want to write CoffeeScript, it is best to write CoffeeScript and compile to JavaScript.

The benefit of TypeScript is mostly design-time benefit and better tooling, so using it in the middle of CoffeeScript and JavaScript adds very little benefit as you will get design time and tooling based on your CoffeeScript code.

You can consume the libraries you write in CoffeeScript in TypeScript and vice-versa, so you can maintain your CoffeeScript libraries in CoffeeScript and consume them in your new TypeScript files while you decide which way to go.

Update: I'm not sure how there can be such a wide misinterpretation of this answer - I'm going to assume that I haven't explained this well (rather than assuming it is merely straw-man argument or hyper-sensitivity to language comparison).

TypeScript is indeed a type system for JavaScript. Static types are more use to you as a programmer earlier in the workflow. Having design-time warnings in your IDE means rapid correction of common errors like mis-typed variable names, incorrect parameters, invalid operations and a whole lot more. Having code underlined and annotated with an error means instant feedback. Having this at compile-time is good, but your feedback loop is longer. I won't even talk about run-time given that all types are erased by this point when using TypeScript.

As to all the "TypeScript vs CoffeeScript" comments - this question is not about that at all. The question is about compiling from CoffeeScript to TypeScript and then to JavaScript. Let's look at why this might not be ideal:

  • You will only get type feedback at compile time
  • You won't get auto-completion
  • Your CoffeeScript code will no longer be compact - it will have type annotations
  • Your CoffeeScript code will no longer be valid without your intermediate compiler
  • You will have to use an additional compiler and it will need to be in-step with CoffeeScript version x and TypeScript version y
  • Your IDE won't understand your CoffeeScript code
like image 31
Fenton Avatar answered Sep 28 '22 04:09

Fenton